[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: sorting non-numeric keys table with custom function ?
- From: Klaus Ripke <paul-lua@...>
- Date: Wed, 24 Feb 2010 01:37:54 +0100
On Tue, Feb 23, 2010 at 09:14:51PM -0300, Luiz Henrique de Figueiredo wrote:
> What do you expect from a sorted table with string keys?
> That pairs will list the entries in order?
> That is not possible: a table is a bag of pairs, not ordered in any way.
unfortunately PHP arrays "work" that way, sort of,
and their asort, arsort, ksort etc hell is an ugly mess *shudder*
Valerio, in Lua it's more like e.g. Perl,
table.sort only works on plain arrays,
i.e. tables indexed by integers 1..n.
"Most functions in the table library assume that the table
represents an array or a list."
http://www.lua.org/manual/5.1/manual.html#5.5
To list a hash sorted by key values,
get the list of keys as plain list, sort that,
and print the hash accordingly as you would in Perl.
for (sort keys %h) { print $_, " -> ", $h{$_}, "\n"; }
well it requires a few more lines in Lua
cheers