WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Table Sort Headache (https://www.wowinterface.com/forums/showthread.php?t=33100)

upyursh 06-11-10 06:13 AM

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?

Waverian 06-11-10 06:34 AM

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.

Chimaine 06-11-10 07:57 PM

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.

Beoko 06-12-10 11:14 AM

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



All times are GMT -6. The time now is 09:15 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI