Arkendorf
Arkendorf

Reputation: 35

Sorting lua lists

I have a list in lua that looks something like this:

list = {{1, 25, 31, 50, 5, 6}, {3, 22, 14, 82, 14, 1}, {2, 13, 40, 67, 92, 12},}

I want to be able to sort it by the first number in each set of braces, so it will become this:

list = {{1, 25, 31, 50, 5, 6}, {2, 13, 40, 67, 92, 12}, {3, 22, 14, 82, 14, 1}}

I have tried table.sort() but that doesn't seem to work. Thanks for all the help!

Upvotes: 0

Views: 111

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474316

table.sort(list, function(a, b) return a[1] < b[1] end)

The second argument to table.sort is a sorting function. So in this case we look into the first item in a and b and use that to compare.

Upvotes: 1

Related Questions