lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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."

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