lua 中table的sort函数排序无法对非连续性索引的表排序

来源:互联网 发布:爱肯拿天峻授权淘宝店 编辑:程序博客网 时间:2024/06/05 14:15
local ifBuff = {}local timestamp = 0;local currNum = 0;function getifname(ifIndex)timestamp = timestamp + 1;    if nil == ifBuff[ifIndex] thenif 10 > currNum thenifBuff[ifIndex] = {ifname = "test"..tostring(ifIndex), expired = timestamp};currNum = currNum + 1;return ifBuff[ifIndex].ifname;endprint("the numb", currNum)table.sort(ifBuff, function(a, b) return a.expired < b.expired end)local removetab = table.remove(ifBuff, 1)ifBuff[ifIndex] = {ifname = "test"..tostring(ifIndex), expired = timestamp};return ifBuff[ifIndex].ifname;endifBuff[ifIndex].expired = ifBuff[ifIndex].expired - 1;    return ifBuff[ifIndex].ifname;endlocal ifIndexRec = {1, 3, 4, 9, 13, 9, 14, 19, 12, 10, 18, 3, 4, 5, 7, 9, 8, 6, 20, 2, 3, 4, 5};for _, n in ipairs(ifIndexRec) dolocal ifnameTmp = getifname(n);print(ifnameTmp);end

原创粉丝点击