Last active
May 18, 2016 13:59
-
-
Save rangercyh/6681359 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--按照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