I'd strongly recommend
not using the function on that linked page, as it wastes memory creating a new table and a new function every time you call it. Something like this should work the same without creating new objects on every call:
Code:
local pairsByKey
do
local temp = {}
local iv, vals = 0
local iter = function()
iv = iv + 1
if temp[iv] == nil then
return nil
else
return temp[iv], vals[temp[iv]]
end
end
local sort = table.sort
function pairsByKey(t)
local i = 1
for k in pairs(t) do
temp[i] = k
i = i + 1
end
for j = i, #temp do
temp[i] = nil
end
sort(temp)
iv, vals = 0, t
return iter
end
end
However, if you find yourself needing a solution like this, I'd be strongly inclined to say you're approaching the problem the wrong way, and there is probably a better way to write your code that doesn't require these kind of tricks.