-- 1. table自定义规则排序示例
guild = {}
table.insert(guild, {name = "Cladhaire", class = "Rogue", level = 70})
table.insert(guild, {name = "Sagart", class = "Prieset", level = 70})
table.insert(guild, {name = "Mallaithe", class = "Warlock", level = 40})
local function sortLevelNameAsc(a, b) -- 自定义排序方法(先按level升序, 再按name升序)
if a.level == b.level then
return a.name < b.name
else
return a.level < b.level
end
end
table.sort(guild, sortLevelNameAsc)
print("1. ----------")
for idx, value in ipairs(guild) do
print(idx, value.level, value.name)
end
-- 2. foreachi和foreach使用
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"}
print("\n2.1 ----------")
table.foreachi(t1, function(i, v) print(i, v) end)
print("2.2 ----------")
table.foreach(t1, function(i, v) print(i, v) end)
-
lua中pairs/ipairs, foreach/foreachi, table自定义排序
最新推荐文章于 2021-07-26 21:37:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)