Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Last active May 18, 2016 13:59
Show Gist options
  • Save rangercyh/6681359 to your computer and use it in GitHub Desktop.
Save rangercyh/6681359 to your computer and use it in GitHub Desktop.
--按照table中数值key的升序或者降序遍历table的迭代器
--nSortType传1为降序,不传默认为升序
--使用示例(降序遍历tbtest):for k, v in gf_PairsByKeys(tbtest, 1) do
function gf_PairsByKeys(tbArray, nSortType)
local tbkey = {}
for key, value in pairs(tbArray) do
if type(key) == "number" then
tbkey[#tbkey+1] = key
end
end
if nSortType == 1 then
table.sort(tbkey, function(a, b) return a > b end)
else
table.sort(tbkey)
end
local i = 0
return function()
i = i + 1
return tbkey[i], tbArray[tbkey[i]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment