Thiago Smith
Thiago Smith

Reputation: 13

Lua with table.sort

local t = {{15,6},{11,8},{13,10}}

I need to show the table in order on the second number

exemple:

1 -> {13,10} -- why 10 > 8

2 -> {11,8} -- why 8 > 6

3 -> {15,6}

Upvotes: 0

Views: 181

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474316

table.sort takes a function that is used to compare the two (it uses < if one isn't provided). So simply pass a function that will be called to do the comparison on the elements.

local t = {{15,6},{11,8},{13,10}}

table.sort(t, function(lhs, rhs) return lhs[2] < rhs[2] end)

Upvotes: 2

Related Questions