Thread Tools Display Modes
04-11-10, 12:06 PM   #1
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Sorting tables in a table

Hello

I currently have a table like this:

instances = {
["name1]" = { 1, other data ... },
["name2"] = { 100, other data ... },
["name3"] = { 10, other data ... },
}

I want to sort this table this way:

instances = {
["name2"] = { 100, other data ... },
["name3"] = { 10, other data ... },
["name1]" = { 1, other data ... },
}

Ofcourse I tried with table.sort and adding my own custom function, but I can't really figure out how to sort tables that are in a table, by a value in that first table
  Reply With Quote
04-11-10, 12:07 PM   #2
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
I tried something like this:

Code:
	function sorty(a,b)
		if (a[1] > b[1]) then return a else return b end
	end

	function ss()
		table.sort(instances, sorty)
	end

Does not work
  Reply With Quote
04-11-10, 12:15 PM   #3
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
You can only sort tables with number indexes so "ipairs()" will work on them i.e.
lua Code:
  1. local function sortfunc(a, b)
  2.     return a[1] > b[1]
  3. end
  4.  
  5. instances = {
  6.     [1] = { 1, "other data"},
  7.     [2] = { 100, "other data"},
  8.     [3] = { 10, "other data"},
  9. }
  10.  
  11. table.sort(instances, sortfunc)
  12.  
  13. for i, v in ipairs(instances) do
  14.     print(i, v[1])
  15. end
  Reply With Quote
04-11-10, 12:49 PM   #4
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
while on the topic, what happens if you have a 0'th index?

does it break the sort? does it get sorted in there just like everything else? do it remain unchanged?
  Reply With Quote
04-11-10, 02:04 PM   #5
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by lilsparky View Post
while on the topic, what happens if you have a 0'th index?

does it break the sort? does it get sorted in there just like everything else? do it remain unchanged?
The 0'th index is ignored as it's not part of the array.
  Reply With Quote
04-11-10, 03:06 PM   #6
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
Originally Posted by Slakah View Post
The 0'th index is ignored as it's not part of the array.
really? hmm... that could really help me clean up some code.
  Reply With Quote
04-12-10, 02:42 AM   #7
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Hmz, so I can not sort my table with named tables ?

Pfft Maybe I'm a little bit short on lua knowledge to accomplish what I want, but for my dps meter (trying to make a custom super lightweight dps-only meter for fun), what I do is the following:

I have an event handler for the combat event, duh
If someone does an attack, I check to see if the player already exists in my table, if so, just add the damage, if not, first create the table-entry, then add the damage in that entry

Then, once a second in my OnUpdate, I make a little frame for each combatant ... with a fontstring ofcourse for the damage and dps.
Next time this onupdate is called, ofcourse I only make a frame for players who don't have a frame yet ...

At the moment, my biggest trouble is to sort the bars... I have to find a way to do the setpoint. This would be easy if the player entries in the table would be sorted according to their damage ofcourse.

Other hard things in a dps mod are: defining pets, friendly fire, and a ****load of other little things, the list grows when you're getting deeper into this

Any thought on the sorting?

Last edited by Sideshow : 04-12-10 at 02:47 AM.
  Reply With Quote
04-12-10, 03:31 AM   #8
corveroth
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 29
If you opt not to use integer indices, your table is treated as a dictionary, not an array. Arrays are ordered, dictionaries are not. Think about it - a dictionary can use any variable type as a key, be it a string, a number, or even a table. How could you have a sensible natural ordering for the elements of something like the following? Which comes first?
Code:
local someDictionary = {
{} = {data, number, string, iDunno},
["4"] = "string",
}
When you have a dictionary, ipairs won't work, because it iterates through, returning the values at consecutive integer keys (starting at 1). You can use pairs to get matched pairs of key, value, but there's no guaranteed order. You probably want to stuff everything into the tables like so:
Code:
instances = {
{ 1, "name1", otherdata, ... },
{ 100, "name2", otherdata, ... },
{ 10, "name3", otherdata, ... },
}
Sort by comparing a[1] and b[1]. If you want to be able to look up the inner tables by the "name"s, you can either iterate through instances until you find it, or maintain a second table - I'd prefer the former.
  Reply With Quote
04-12-10, 04:53 AM   #9
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Originally Posted by corveroth View Post
Code:
instances = {
{ 1, "name1", otherdata, ... },
{ 100, "name2", otherdata, ... },
{ 10, "name3", otherdata, ... },
}
That would work indeed. The problem is, that my combat-event-function will be a lot slower, since I'll have to search the index where [2] = "nameX".

Right now I just do mytable["theplayername"] = blabla damage numbers blabla. No searching needed.
  Reply With Quote
04-12-10, 04:59 AM   #10
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Sideshow View Post
That would work indeed. The problem is, that my combat-event-function will be a lot slower, since I'll have to search the index where [2] = "nameX".

Right now I just do mytable["theplayername"] = blabla damage numbers blabla. No searching needed.
Theres nothing stoping you from keeping a playername to damage numbers table as well as the sorting table.
  Reply With Quote
04-12-10, 01:25 PM   #11
Shadowed
...
Premium Member
Featured
Join Date: Feb 2006
Posts: 387
Originally Posted by Slakah View Post
Theres nothing stoping you from keeping a playername to damage numbers table as well as the sorting table.
Just remember to be mindful of how you are looping through a table, if you only want to get the indexed portions you're sorting on, you would have to do for i=1, #(tbl) do end rather than a pairs loop.
  Reply With Quote
04-12-10, 02:35 PM   #12
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Ok, two tables then ...

I just totally don't get this...

I make a table for easy use in the eventhandler:
player["sideshow"][1] = 500 -- adding the damage done, offcourse the "string" will be an "arg" and not "sideshow"

I make another table for the statusbars....
bar = {
{ 500, "sideshow" },
{ 8000, "anothername" },
{ 987456, "anothername2" },
}
Ok, I can sort this. But how and when am I supposed to update the damagnumber?
This looks like a very bad idea and very slow to iterate through to find the right name, and then update the number

Last edited by Sideshow : 04-12-10 at 02:40 PM.
  Reply With Quote
04-12-10, 03:22 PM   #13
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
lua Code:
  1. local instances = {
  2.     name1 = {1, "otherdata"},
  3.     name2 = {100, "moredata"},
  4.     name3 = {10, "dataz"},
  5. }
  6.  
  7. local orderedinstances = {"name1", "name2", "name3"}
  8.  
  9. local function sortfunc(a, b)
  10.     return instances[a][1] > instances[b][1]
  11. end
  12.  
  13. table.sort(orderedinstances, sortfunc) --sort oredered instances
  14.  
  15. --iterate through instances in the proper order
  16. for i, name in ipairs(orderedinstances) do
  17.     local instanceobj = instances[name]
  18.    
  19.     --do stuff with instanceobj
  20. end

Something like that. You can much about with the numbers in instances then sort orderedinstances then iterate over your nested tables.

If I understand your problem.
  Reply With Quote
04-12-10, 04:00 PM   #14
Sideshow
A Flamescale Wyrmkin
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 103
Hmz, I made it work. I had to add a reference to the same statusbar objects in both tables.

Thanks all
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Sorting tables in 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