@leviyuan
2017-08-11T08:29:15.000000Z
字数 1641
阅读 312
supermobs lua utils
字典中的key是要排序的数据
value是排序的依据,类型为table数组,每项值为number类型
-- 创建一个排序器,排序依据的table有最多4个值-- 依次按照升序、升序、降序、降序的方式排列local compare = utils.table.compare.makearr(true, true, false, false)-- 使用这个排序器创建一个orderdictlocal dict = utils.table.createorderdict(compare)-- 添加一些数据dict["chiuan"] = {28, 15, 236, 0}dict["levi"] = {27, 0, 0, 0}dict["test"] = {28, 15, 0, 0}-- 调用接口测试一下-- 数据的数量print(dict:getlen()) -- 3-- 获取排序在第2位的数据print(dict:getat(2)) -- test-- 完整遍历一下for k,v in dict:ipairs() doprint(k, v) -- 1,levi 2,test 3,chiuanend-- 删除一项dict["test"] = nil-- 下面这样会导致error “orderdict do not support same order value”dict["test_error"] = {27, 0, 0, 0}
与原生table基本一样,唯一差别就是保证pairs的遍历顺序永远与添加进去的顺序一致
-- 创建一个有序字段local dict = utils.table.createsortdict()-- 添加一些数据dict["levi"] = 27dict["chiuan"] = {}dict["test"] = true-- 使用接口测试一下print(dict:getfirst()) -- 27print(dict.count) -- 3-- 完整遍历for k,v in dict:pairs() doprint(k, v) -- levi,27 chiuan,table0x0000 test,trueend-- 删除或更新会让这一项移动到最末位dict["levi"] = 1print(dict:getfirst()) -- table0x0000dict["chiuan"] = {}print(dict:getfirst()) -- true