0
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}

2
  • You need to set up comparator properly Commented Aug 24, 2012 at 16:14
  • -1: Welcome to Stack Overflow! We generally like it when people make some effort to solve the problem on their own before asking. In your case, all you needed to do was look up table.sort in the Lua documentation. Commented Aug 24, 2012 at 16:17

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.