Thread Tools Display Modes
06-06-08, 01:00 PM   #1
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Table Sorting

Is there anyway to sort a table with none numeric keys.

i.e.

Code:
Fader = {
Combat = 0.8,
PlayerHostileTarget = 0.9,
PlayerTarget = 0.5,
UnitTarget = 0.5,
UnitHostileTarget = 0.1,
Normal = 0.2
}
would become

Code:
Fader = {
PlayerHostileTarget = 0.9,
Combat = 0.8,
PlayerTarget = 0.5,
UnitTarget = 0.5,
Normal = 0.2
UnitHostileTarget = 0.1,
}
  Reply With Quote
06-06-08, 03:29 PM   #2
Jzar
A Chromatic Dragonspawn
 
Jzar's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 158
I'm not sure about the intricacies, but I think table.sort() will do it. The doc I'm looking at is here (http://lua-users.org/wiki/TableLibraryTutorial), but it doesn't say anything about behavior for non-numerical keys, and I'm here at work, so no way to test right now.

If table.sort() by itself doesn't do it, you might be able to make it work properly with a custom sorting function, based on the example in the table.sort entry in the doc linked above.

I would be wary of using table keys as data, though. The whole point of keys being any data type is to make lookups easier. You might try making two tables and synchronizing their sort, however.

Good luck!
  Reply With Quote
06-06-08, 03:55 PM   #3
Shirik
Blasphemer!
Premium Member
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2007
Posts: 818
There is no meaning to "sorting non-numeric keys." Neither next() nor pairs() guarantees any order, and there is no other way to traverse over the table.

In short: the answer is no because "yes" has no meaning.
__________________
たしかにひとつのじだいがおわるのお
ぼくはこのめでみたよ
だけどつぎがじぶんおばんだってことわ
しりたくなかったんだ
It's my turn next.

Shakespeare liked regexes too!
/(bb|[^b]{2})/
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Table Sorting


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off