Sort Array in Lua

Hi, I am very new to Lua, I need to sort the array in Lua.

So I have the following code

local distances = {2,3,1}
table.sort(distances)

Now I understand

>distance[1 ] –> 1
>Distance[2] –> 2
>Distance[3] –> 3

Now I need to save some information for my “distance”
Similar to the following Content

local distances = {{C1,2},{C2,3},{C3,1}}

It is now impossible to call sort- function, but I need to sort them.
Is it possible to achieve this goal?

>Distance[1] –> {} C3,1
>Distance[2] –> {} C2,2
>Distance[3] –> {} C1,3< /p>

Thank you:)

table.sort Take the comparison function as its second parameter.

table.sort(distances, function (left, right)
return left[2] end)

Hi, I am very new to Lua, and I need to sort the array in Lua.

< p>So I have the following code

local distances = {2,3,1}
table.sort(distances)

Now I Understood

>Distance[1] –> 1
>Distance[2] –> 2
>Distance[3] –> 3

Now I need to My “distance” saves some information
similar to the following

local distances = {{C1,2},{C2,3},{C3,1}} 

It is impossible to call sort-function now, but I need to sort them.
Is it possible to achieve this goal?

>Distance[1] –> {} C3,1
>Distance[2] –> {} C2,2
>Distance[3] –> {} C1,3< /p>

Thank you:)

table.sort takes the comparison function as its second parameter.

table.sort(distances, function (left, right)
return left[2] end)

Leave a Comment

Your email address will not be published.