在使用 pairs 遍历一些 table时,针对key 值按一定顺序输出结果。
function pairsByKeys(t)
local a = {}
for n in pairs(t) do
a[#a+1] = n
end
table.sort(a)
local i = 0
return function()
i = i + 1
return a[i], t[a[i]]
end
end
使用方法:
for k,v in pairsByKeys(tbl) do
print(k,v)
end