WoW:API sort: Difference between revisions

41 bytes added ,  28 August 2016
m
({{luaapi}})
Line 42: Line 42:


  function pairsByKeys (t, f)
  function pairsByKeys (t, f)
local a = {}
    local a = {}
for n in pairs(t) do table.insert(a, n) end
    for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
    table.sort(a, f)
local i = 0      -- iterator variable
    local i = 0      -- iterator variable
local iter = function ()  -- iterator function
    local iter = function ()  -- iterator function
i = i + 1
        i = i + 1
if a[i] == nil then return nil
        if a[i] == nil then return nil
else return a[i], t[a[i]]
        else return a[i], t[a[i]]
end
        end
end
      end
return iter
      return iter
  end
  end


Line 58: Line 58:


  for title,value in pairsByKeys(randomtable) do
  for title,value in pairsByKeys(randomtable) do
DEFAULT_CHAT_FRAME:AddMessage(title..", "..value);
    DEFAULT_CHAT_FRAME:AddMessage(title..", "..value);
  end
  end


This will print all the variables in randomtable alphabetically!
This will print all the variables in randomtable alphabetically!