lua-users home
lua-l archive

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


My program (a World of Warcraft addon) uses a number
of key-value tables.  I'm trying to output them sorted
by value.  I can't seem to get them into any
recognizable order.

I tried table.sort(tbl) and tbl.sort(tbl,
function(a,b) return a[2] > b[2]; end).  I don't know
if my table is not getting sorted, or if pairs() is
just messing up the order.  (I know pairs() doesn't
guarantee the order.)

After reading some stuff, I came up with this: instead
of tbl["key"] = value, I could explicitly create a
table of tables, something like tbl.insert({foo="key",
bar=value}).  Then I could table.sort(tbl,
function(a,b) return a.bar > b.bar; end).  If I
understand correctly, the main table would have an
implicit numeric index which I could iterate over with
ipairs().

Before I rewrite all my code, I'm hoping someone can
tell me if this will work...

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com