Thread Tools Display Modes
02-07-06, 03:21 PM   #1
plamalice
A Defias Bandit
Join Date: Jan 2006
Posts: 2
Question regarding table.sort

Hi, im pretty new to lua. Just coded an addon but im having trouble with the sorting function.

I have a table built like this :
Code:
PlayerDB = { name, class, level }

PlayerDB.name = { "name1", "name2", "name3" }
PlayerDB.class = { "Hunter", "Priest", "Mage" }
PlayerDB.level = { "30", "20", "10" }
now, if i use the table.sort function on PlayerDB.level, the values get sorted in-place of the current values. name and class though will remain unsorted.

Basically, what im trying to figure out, is if there is a way of table.sort returning me the sorting order (what original index is now index 1,2,3). So that i can then sort the other fields accordingly.

Or is my only option to code my own sorting function ?
  Reply With Quote
02-07-06, 05:09 PM   #2
wereHamster
A Black Drake
 
wereHamster's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 80
wouldn't be a better design look like:

Code:
PlayerDB = {
   { name = "name1", class = "class1", level = 30 }
   { name = "name2", class = "class2", level = 40 }
   { name = "name3", class = "class3", level = 50 }
}

table.sort(PlayerDB,
   function(leftEntry, rightEntry)
      return leftEntry.level < rightEntry.level
   end
)
  Reply With Quote
02-07-06, 05:14 PM   #3
plamalice
A Defias Bandit
Join Date: Jan 2006
Posts: 2
That's exactly what i needed. Thank you

Gonna give it a try later...

Update : Made the changes. Working great. thanks for the help

Last edited by plamalice : 02-08-06 at 01:41 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Question regarding table.sort


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