lua table的一个排序算法
lines = {
luaH_set = 10,
luaH_get = 24,
luaH_present = 48,
}
function pairsByKeys (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
f=function(a,b)
if a > b then return true end
end
for name, line in pairsByKeys(lines,f) do
print(name, line)
end
luaH_set = 10,
luaH_get = 24,
luaH_present = 48,
}
function pairsByKeys (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
f=function(a,b)
if a > b then return true end
end
for name, line in pairsByKeys(lines,f) do
print(name, line)
end
![](http://www.cnblogs.com/Emoticons/baimantou/202015934.gif)