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

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

Asked by 5 years ago

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

Log in to vote
1
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

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
0
Alright thanks! I didn't realize that :P ScriptingCon 52 — 5y
0
No problem bro, have fun coding! aazkao 787 — 5y
Ad

Answer this question