Kinda new to Lua and trying to sort aceDB tables with no success.
I have the following table definition:
default_options = {
global = {
data = {
-- Faction
['*'] = {
-- Realm
['*'] = {
-- Name
['*'] = {
class = "",
}
}
}
},
},
}
and am trying to do this:
function resortDB()
print ("Starting Sort Action")
local sfaction
for faction, faction_table in pairs (self.db.global.data) do
sfaction = faction
for realm, realm_table in pairs (faction_table) do
for pc in pairs (realm_table) do
table.sort(self.db.global.data[sfaction][realm], function(a,b)
if self.db.profile.options.sort_type == "alpha" then
return a > b
elseif self.db.profile.options.sort_type == "rev-alpha" then
return a < b
elseif ....
end
end )
end
end
end
end
What I want to be able to do is sort the Characters. If i can get the alpha and reverse alpha to work i can then do a few other options.
This is an dictionnary, so you cannot sort it. At best you can fill another table to iterate over the names in the right order. E.g. :
local function revAlphaSort(a,b)
return b < a
end
function addon:FetchOrderedNames(names, characters)
wipe(names)
for name in pairs(characters) do
table.insert(names, name)
end
if self.db.profile.options.sort_type == "alpha" then
table.sort(names)
elseif self.db.profile.options.sort_type == "rev-alpha" then
table.sort(names, revAlphaSort)
end
end
-- To iterate through all faction/realm/names
local names = {}
for faction, realms in pairs(addon.db.global.data) do
for realm, characters in pairs(realms) do
addon:FetchOrderedNames(names, characters)
for i, name in ipairs(names) do
-- do whatever with name
end
end
end
PS: don't put anonymous function declaration in a loop: in Lua this will create as many function instances as times the loop gets executed.
Kinda new to Lua and trying to sort aceDB tables with no success.
I have the following table definition:
and am trying to do this:
What I want to be able to do is sort the Characters. If i can get the alpha and reverse alpha to work i can then do a few other options.
You cannot sort a Dictionary in Lua. Only indexed tables.
PS: don't put anonymous function declaration in a loop: in Lua this will create as many function instances as times the loop gets executed.