Hi i am quite new to lua and i need to sort an Array in Lua.
你好,我对lua很陌生,我需要对lua中的数组进行排序。
So i have following code
我有下面的代码
local distances = {2,3,1}
table.sort(distances)
now i get
现在我得到
- distances[1] -> 1
- 距离[1]- > 1
- distances[2] -> 2
- 距离[2]- > 2
- distances[3] -> 3
- 距离[3]- > 3
now i need to save some information for my "distances" aswell something like the following
现在我需要为我的“距离”保存一些信息,如下所示
local distances = {{C1,2},{C2,3},{C3,1}}
now it is impossible to call the sort-function, but i need them sorted. Is it possible to reach this?
现在不可能调用sort函数,但我需要对它们排序。有可能达到这个目标吗?
- distances[1] -> {C3,1}
- 距离[1]- > { C3 1 }
- distances[2] -> {C2,2}
- 距离[2]- > { C2,2 }
- distances[3] -> {C1,3}
- 距离[3]- > { C1,3 }
Thanks guys :)
谢谢大家:)
1 个解决方案
#1
4
table.sort
takes a comparison function as its second argument.
表。排序的第二个参数是比较函数。
table.sort(distances, function (left, right)
return left[2] < right[2]
end)
#1
4
table.sort
takes a comparison function as its second argument.
表。排序的第二个参数是比较函数。
table.sort(distances, function (left, right)
return left[2] < right[2]
end)