Thread Tools Display Modes
06-11-10, 06:13 AM   #1
upyursh
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 32
Table Sort Headache

Im getting a headache with this haha

I have a table of raid subs, but of course they are added to the table as they come in.

Table is like this..

Code:
		["subsList"] = {
			["AToonName"] = {
				["toon"] = "AToonName",
				["note"] = "",
				["updatednice"] = "20:38",
				["addednice"] = "20:38",
				["added"] = 1276254517,
				["updated"] = 1276254517,
			},
			["DToonName"] = {
				["toon"] = "DToonName",
				["note"] = "hmmm",
				["updatednice"] = "20:38",
				["addednice"] = "20:38",
				["added"] = 1276254503,
				["updated"] = 1276254530,
			},
			["AToonName2"] = {
				["toon"] = "AToonName2",
				["note"] = "yo",
				["updatednice"] = "20:39",
				["addednice"] = "20:38",
				["added"] = 1276254528,
				["updated"] = 1276254553,
			},
			["NToonName"] = {
				["toon"] = "NToonName",
				["note"] = "woah",
				["updated"] = 1276254802,
				["added"] = 1276254517,
				["addednice"] = "20:38",
			},
			["KToonName"] = {
				["toon"] = "KToonName",
				["note"] = "",
				["updatednice"] = "20:39",
				["addednice"] = "20:38",
				["added"] = 1276254521,
				["updated"] = 1276254574,
			},
		},
Want it to sort it by the Toon Name could either be by the index or the toon data

Any ideas?
  Reply With Quote
06-11-10, 06:34 AM   #2
Waverian
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 188
You can't reliably sort a hash table because the returns you receive when iterating it aren't necessarily going to be in sorted order. Use an array instead, or add an additional data structure.

Code:
subsList = {
	-- etc
}

-- Copy character names to sorting array.
local sorted = {}

for name in pairs(subsList) do
	sorted[#sorted + 1] = name
end

table.sort(sorted, function(a, b) return a > b end)
Then you would use this to get a sorted value from your hash table.

Code:
local first = subsList[sorted[1]]
But really since you already have a value in your table for character name it's probably better to just switch over to an array.
  Reply With Quote
06-11-10, 07:57 PM   #3
Chimaine
A Deviate Faerie Dragon
 
Chimaine's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 17
Or you just make it an array from the start...

Code:
		["subsList"] = {
			{
				["toon"] = "AToonName",
				["note"] = "",
				["updatednice"] = "20:38",
				["addednice"] = "20:38",
				["added"] = 1276254517,
				["updated"] = 1276254517,
			},
			{
				["toon"] = "DToonName",
				["note"] = "hmmm",
				["updatednice"] = "20:38",
				["addednice"] = "20:38",
				["added"] = 1276254503,
				["updated"] = 1276254530,
			},
			{
				["toon"] = "AToonName2",
				["note"] = "yo",
				["updatednice"] = "20:39",
				["addednice"] = "20:38",
				["added"] = 1276254528,
				["updated"] = 1276254553,
			},
			{
				["toon"] = "NToonName",
				["note"] = "woah",
				["updated"] = 1276254802,
				["added"] = 1276254517,
				["addednice"] = "20:38",
			},
			{
				["toon"] = "KToonName",
				["note"] = "",
				["updatednice"] = "20:39",
				["addednice"] = "20:38",
				["added"] = 1276254521,
				["updated"] = 1276254574,
			},
		},
Then you just can do
Code:
table.sort(sometable.subsList, function(a,b) return a.toon > b.toon end)
Since you don't need the name as a key if you already store it in the subtable anyway. It's just redundant. If you use your keys somewhere in your code, just think hard, there is always another way.
__________________
Author cAddOns
Chimaine on EU-Destromath.

ATTN! A german writing english!
  Reply With Quote
06-12-10, 11:14 AM   #4
Beoko
Guest
Posts: n/a
Out of curiosity: may I ask why you are storing timestamps in two different formats? If it's for GUI purposes, you could always pass "added" and "updated" (or time() itself) as the second argument to date().

Code:
date("%I:%M:%S", time()) --> 01:06:56
date("%I:%M:%S", 1276362416) --> 01:06:56
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Table Sort Headache

Thread Tools
Display Modes

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