Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to sort the order of a table of numbers and print it?

Asked by
obcdino 113
6 years ago

I have a list of numbers in a table I want to sort from least to greatest, how would I go about doing that?

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

That's what I have, but I'm not sure what to do from there.

1 answer

Log in to vote
0
Answered by 6 years ago

The second parameter of table.sort is a function that compares two numbers of the entire table and evaluates their position

local t = {1, 3, 2}
table.sort(t, function(a, b) return a < b end)

This will say "okay so 1 < 3 which means it should stay where it is"

If you would like to make it go from greatest to least, just change the "<" to a ">"

Ad

Answer this question