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

Is it possible to sort a table from highest to lowest instead of lowest to highest?

Currently I'm trying to make a table for holding values. But when I use table.sort it just makes the highest go to the end(which I understand that's the intention with it).. but is there a way to make it sort from highest to lowest?

1 answer

✓ Accepted answer

Yes, and believe it or not its using the same function table.sort

If you dont define a comparator in the sort function it will always default to less than, which sorts from lowest to highest, to add a comparator just add a function in the second argument that returns if the value is higher

local table1 = {10,34,1,23}

table.sort(table1,function(a,b)
	return a>b
end)
for i,v in pairs(table1) do
	print(v)
end

2 comments

0

Alright thanks! I didn't realize that :P

0

No problem bro, have fun coding!

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