Thread Tools Display Modes
09-21-08, 09:00 AM   #1
Travisstorma
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 9
Sorting a table.

I am almost finished with something that I am working on. However, I cannot for the life of me figure out how to sort a table from low to high. I probably did this a little wrong to begain with, but it works, besides me not figuring out how to sort the info.
What I have is a timer that has 5 possible bars. Sometimes only one is show sometimes all the way up to five are shown. I have five frames that are hidden unless there is info to be displayed in them. However, I would like the bars to be ordered top to bottom from lowest timer value to highest timer value, with the nil valued frames hidden at the bottom.
Here is my table structure.
Code:
myaddon.framestuff {
          Frame1 = {
                    Timer = var
                    Target = var
                    Subtract = var
                    Who = var
          }
          Frame2 = {
                    Timer = var
                    Target = var
                    Subtract = var
                    Who = var
          }
          Frame3 = {
                    Timer = var
                    Target = var
                    Subtract = var
                    Who = var
          }
          Frame4 = {
                    Timer = var
                    Target = var
                    Subtract = var
                    Who = var
          }
          Frame5 = {
                    Timer = var
                    Target = var
                    Subtract = var
                    Who = var
          }
}
So basically what would have to happen is I would have to have a function go through and see what frame.Timer values are and transfer the info to another frame. However, I am probably way off on my thinking.

Thanks to whoever takes the time to read this!

--Travis (some gnome mage)
  Reply With Quote
09-21-08, 09:14 AM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
table.sort is what you're looking for: http://www.lua.org/manual/5.1/manual...pdf-table.sort

Code:
table.sort (table [, comp])
If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second
In your case this could be:
Code:
table.sort(myaddon.framestuff, function(a, b) if a.Timer < b.Timer then return true end)
  Reply With Quote
09-21-08, 10:21 AM   #3
Travisstorma
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 9
Originally Posted by Duugu View Post
table.sort is what you're looking for: http://www.lua.org/manual/5.1/manual...pdf-table.sort

Code:
table.sort (table [, comp])


In your case this could be:
Code:
table.sort(myaddon.framestuff, function(a, b) if a.Timer < b.Timer then return true end)
I figured it would be something close to this. My problem is how do I acutally go about making it sort the table? If I throw this in a function and call it, it does nothing.
  Reply With Quote
09-21-08, 10:49 AM   #4
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
It's about keys and values. I'm almost sure http://www.lua.org/pil/19.3.html will help you with this.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Sorting a table.


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