I am making a leaderboard system where it sorts a player’s rank based on how long they survived in the round they played in. I can already set the player’s rank but it is set as a value in a dictionary table under the player.
So here’s the question: How do I sort a table based off a player’s rank. Sorry for not understanding much table terminology
-- The rank is the first value in each player, and I want to sort the 'leaderboardList' table like this
local leaderboardList = {
[player2] = 1,
[player3] = 2,
[player1] = 3
}
table.sort(leaderboardList, function(elementA, elementB)
--the elements are the values of the keys. We can compare them to sort the table.
return elementA > elementB
end)
It doesn’t. Run this code and you’ll see the behavior:
--!strict
local function generateRandomDictionary(count: number): {[string]: number}
local dictionary: {[string]: number} = {}
for i: number = 1, count do
dictionary[tostring(i)] = math.random(1, 100)
end
return dictionary
end
local randomDictionary: {[string]: number} = generateRandomDictionary(100)
-- this will typecheck error as it is expecting an array
table.sort(randomDictionary, function(a: number, b: number): boolean
return a > b
end)
for index: string, value: number in randomDictionary do
print(index, value)
end