Lua:在两个特定列上迭代 table(使用字母数字值)
Lua: iterate table (with alphanumeric values) on two specific columns
我有这段代码迭代 Lua table“childrenTable”来检索它的值,但我希望它们按列 排序 "symbolicname" 然后,按列 "name"
- 这个例子中的table内容是插入的,但在我的真实场景中它们是自动填充的,所以我无法控制它们如何进入table。我将在此处插入内容只是为了填充 table:
local JSON = require 'dkjson'
local childrenTable = {}
childrenTable[1] = {symbolicname = "02 CONNECTOR", path = "system/core/b connector", name = "b connector"}
childrenTable[2] = {symbolicname = "02 CONNECTOR", path = "system/core/a connector", name = "a connector"}
childrenTable[3] = {symbolicname = "01 CORE", path = "/system/core/b", name = "b"}
childrenTable[4] = {symbolicname = "01 CORE", path = "/system/core/a", name = "a"}
for index,value in ipairs(childrenTable) do print(JSON.encode(value)) end
这是 return 的结果:
{"name":"b connector","symbolicname":"02
CONNECTOR","path":"system/core/b connector"}
{"name":"a connector","symbolicname":"02
CONNECTOR","path":"system/core/a connector"}
{"name":"b","symbolicname":"01 CORE","path":"/system/core/b"}
{"name":"a","symbolicname":"01 CORE","path":"/system/core/a"}
但我希望它按顺序 return 列表:
{"name":"a","symbolicname":"01 CORE","path":"/system/core/a"}
{"name":"b","symbolicname":"01 CORE","path":"/system/core/b"}
{"name":"a connector","symbolicname":"02
CONNECTOR","path":"system/core/a connector"}
{"name":"b connector","symbolicname":"02
CONNECTOR","path":"system/core/b connector"}
提前致谢,
罗杰
使用table.sort
函数,它会遍历table并在当前索引的左右翼传递给你一个项目。 (table.sort
基于快速排序算法)
这对你有用:
local childrenTable = {}
childrenTable[1] = {symbolicname = "02 CONNECTOR", path = "system/core/b connector", name = "b connector"}
childrenTable[2] = {symbolicname = "02 CONNECTOR", path = "system/core/a connector", name = "a connector"}
childrenTable[3] = {symbolicname = "01 CORE", path = "/system/core/b", name = "b"}
childrenTable[4] = {symbolicname = "01 CORE", path = "/system/core/a", name = "a"}
table.sort(childrenTable, function(a, b)
if a.symbolicname < b.symbolicname then
return true
else
if a.name < b.name then
return true
else return false end
end
return false
end)
for k,v in pairs(childrenTable) do print(k, v.name, v.symbolicname) end
此外,对于大量数据,请考虑使用数据框。
我有这段代码迭代 Lua table“childrenTable”来检索它的值,但我希望它们按列 排序 "symbolicname" 然后,按列 "name"
- 这个例子中的table内容是插入的,但在我的真实场景中它们是自动填充的,所以我无法控制它们如何进入table。我将在此处插入内容只是为了填充 table:
local JSON = require 'dkjson'
local childrenTable = {}
childrenTable[1] = {symbolicname = "02 CONNECTOR", path = "system/core/b connector", name = "b connector"}
childrenTable[2] = {symbolicname = "02 CONNECTOR", path = "system/core/a connector", name = "a connector"}
childrenTable[3] = {symbolicname = "01 CORE", path = "/system/core/b", name = "b"}
childrenTable[4] = {symbolicname = "01 CORE", path = "/system/core/a", name = "a"}
for index,value in ipairs(childrenTable) do print(JSON.encode(value)) end
这是 return 的结果:
{"name":"b connector","symbolicname":"02
CONNECTOR","path":"system/core/b connector"}
{"name":"a connector","symbolicname":"02
CONNECTOR","path":"system/core/a connector"}
{"name":"b","symbolicname":"01 CORE","path":"/system/core/b"}
{"name":"a","symbolicname":"01 CORE","path":"/system/core/a"}
但我希望它按顺序 return 列表:
{"name":"a","symbolicname":"01 CORE","path":"/system/core/a"}
{"name":"b","symbolicname":"01 CORE","path":"/system/core/b"}
{"name":"a connector","symbolicname":"02
CONNECTOR","path":"system/core/a connector"}
{"name":"b connector","symbolicname":"02
CONNECTOR","path":"system/core/b connector"}
提前致谢,
罗杰
使用table.sort
函数,它会遍历table并在当前索引的左右翼传递给你一个项目。 (table.sort
基于快速排序算法)
这对你有用:
local childrenTable = {}
childrenTable[1] = {symbolicname = "02 CONNECTOR", path = "system/core/b connector", name = "b connector"}
childrenTable[2] = {symbolicname = "02 CONNECTOR", path = "system/core/a connector", name = "a connector"}
childrenTable[3] = {symbolicname = "01 CORE", path = "/system/core/b", name = "b"}
childrenTable[4] = {symbolicname = "01 CORE", path = "/system/core/a", name = "a"}
table.sort(childrenTable, function(a, b)
if a.symbolicname < b.symbolicname then
return true
else
if a.name < b.name then
return true
else return false end
end
return false
end)
for k,v in pairs(childrenTable) do print(k, v.name, v.symbolicname) end
此外,对于大量数据,请考虑使用数据框。