Here's a function I use all the time in my addons to sort tables that dont have key values:
Code:
function Sorted(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
then instead of calling
Code:
for k, v in pairs(table) do
you call
Code:
for k, v in Sorted(table) do
You can pass a function as f to determine how you want it sorted too. Look at
http://lua-users.org/wiki/TableLibraryTutorial for more info about that. Hope this helps.