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

How to table.sort a table from highest to lowest?

hello, I'm making a leader board type thing , and i would like to know how to sort a table form highest to lowest, i know about table.sort, but it sorts it from lowest to highest...so is there a way to sort it form highest to lowest?

thanks

1 comment

0

Of course there is a way. Just type table.sort in your script and read the description carefully

1 answer

✓ Accepted answer

table.sort contains a predicational argument. This is a callback function that is be used to condition the sorting, and can allow us to redefine the attributes' evaluation during the process.

The technical jargon may be confusing, but I assure you the application is a lot simpler than most would expect. The function is provided with the current & next enumerating attributes—the two tokens that are compared for smallest, and sorted accordingly. All we must do, is reverse the Boolean expression:

local arbitraryTable = {4,2,6,3,7,5}

table.sort(arbitraryTable, function(a,b)
    return a > b --// Previously evaluated as <
end)

print(unpack(arbitraryTable)) --// 7,6,5,4,3,2

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