Thread Tools Display Modes
10-23-10, 06:47 PM   #1
Waky
A Cobalt Mageweaver
 
Waky's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2010
Posts: 200
Sorting tables by value

I've been trying to sort a table by the value, rather than the key. This will be for my addon that tracks memory usage of addons, so it has the key as a name and the value as the memory (in KB.)

The code I'm currently using is:
Code:
table.sort(addonsMem, (function(a, b) return a[2] > b[2] end))

for k, v in pairs(addonsMem) do 
GameTooltip:AddDoubleLine(k, (wTools_memFormat(v)),1,1,1,1,1,1)
end
I've seen how to highlight by Lua but I've since forgotten, can someone tell me that [Bracket Code] again? Lol thanks :P

I can't get this code to work right, does anyone know how to?

Thanks in advance!
  Reply With Quote
10-23-10, 07:34 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Without knowing what your table looks like, it's impossible to say.

If it's a standard key/value table, though, table.sort does nothing. You can only sort indexed tables.
  Reply With Quote
10-23-10, 08:13 PM   #3
Waky
A Cobalt Mageweaver
 
Waky's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2010
Posts: 200
Originally Posted by Phanx View Post
Without knowing what your table looks like, it's impossible to say.

If it's a standard key/value table, though, table.sort does nothing. You can only sort indexed tables.
I can't believe I didn't realize what I was doing. The keys were strings, not numbers so it didn't work. Instead of using the name as the key, I left the key as is and made the value into a table, and it worked!

Thanks for bringing me to reality Phanx!
  Reply With Quote
10-23-10, 09:37 PM   #4
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Originally Posted by Waky View Post
PHP Code:
table.sort(addonsMem, (function(ab) return a[2] > b[2end))
for 
kv in pairs(addonsMem) do 
    
GameTooltip:AddDoubleLine(k, (wTools_memFormat(v)),1,1,1,1,1,1)
end 
I've seen how to highlight by Lua but I've since forgotten, can someone tell me that [Bracket Code] again? Lol thanks :P
There's no lua tag that I know off but people fake it with [php]your code here[/php]

Last edited by Dridzt : 10-23-10 at 09:42 PM.
  Reply With Quote
10-23-10, 09:42 PM   #5
Waky
A Cobalt Mageweaver
 
Waky's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2010
Posts: 200
Originally Posted by Dridzt View Post
There's no lua tag that I know off but people fake it with [php]your code here[/php]
Thanks, I forgot what it was lol
  Reply With Quote
10-23-10, 11:15 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Dridzt View Post
There's no lua tag that I know off but people fake it with [php]your code here[/php]
The [code] tag is more readable, though. Lime green and blue on the yellowish background isn't exactly the paragon of legibility.
  Reply With Quote
10-24-10, 01:19 AM   #7
voodoodad
Large, Friendly Letters!
 
voodoodad's Avatar
Join Date: Oct 2008
Posts: 1,632
Originally Posted by Phanx View Post
The [code] tag is more readable, though. Lime green and blue on the yellowish background isn't exactly the paragon of legibility.
Doesn't work well for the dark theme either, believe me...
__________________

~ no need to make the message completely obnoxious - Cairenn
  Reply With Quote
10-24-10, 01:23 AM   #8
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
Originally Posted by Dridzt View Post
There's no lua tag that I know off but people fake it with
lua Code:
  1. table.sort(addonsMem, (function(a, b) return a[2] > b[2] end))
  2.  
  3. for k, v in pairs(addonsMem) do
  4. GameTooltip:AddDoubleLine(k, (wTools_memFormat(v)),1,1,1,1,1,1)
  5. end

[highlight='lua']
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
10-24-10, 01:23 AM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Waky View Post
I've seen how to highlight by Lua but I've since forgotten, can someone tell me that [Bracket Code] again? Lol thanks :P
I use this for Lua syntax highlighting.

Code:
[highlight=lua]
-- Example code
print("blarg",0);
[/highlight]
lua Code:
  1. -- Example code
  2. print("blarg",0);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Sorting tables by value


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