Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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 ">"

This discussion is preserved as it was. New answers, comments, and votes are closed.