# Need help on tables

Hello everyone, I want to make a leaderboard for a school game, but I need help because it is not arranged in order (the numbers).

How can I arrange in ascending order this type of table :

``````local TheTable = {
["Player1"] = 3,
["Player2"] = 1,
["Player3"] = 10,
["Player4"] = 5,
}
``````

maybe this?

``````table.sort(TheTable)
``````

I am trying this, thank you for the fast answer

1 Like

You can use table.sort()

``````table.sort(TheTable, function(a, b)
return a > b
end)
``````

This donâ€™t work, when I try to print the table, it return nil

I am trying, thank you too (30)

``````local TheTable = {
Player1 = 3,
Player2 = 1,
Player3 = 10,
Player4 =  5,
}

function sortTable()
table.sort(TheTable, function(a, b)
return a > b
end)
end

sortTable()
``````

Here the output, it donâ€™t work,

I am trying this, hope this work : )

It donâ€™t work, is this correct :

``````local TheTable = {
Player1 = 3,
Player2 = 1,
Player3 = 10,
Player4 =  5,
}

print(TheTable)

function sortTable()
table.sort(TheTable, function(a, b)
return a > b
end)
end

sortTable()

print(TheTable)

print(sortTable())
``````

The print of sortTable is nil (30)

Everybody is suggesting array sorting, which doesnâ€™t work in this instance (you have a dictionary). The solution is simple: make an array from your dictionary, and sort the array. Iâ€™ll explain why using a dictionary here doesnâ€™t work, but first - hereâ€™s a function for that:

``````local function sortDict(dict: { [any]: any })
local arr = {}
for key, value in pairs(dict) do
table.insert(arr, { Key = key, Val = value })
end

table.sort(arr, function(a, b)
return a.Val > b.Val
end)

return arr
end
``````

The reason your output isnâ€™t sorted is simple: dictionaries donâ€™t have an order. They use a HashMap/HashTable (not sure which) behind the scenes, so they have some intrinsic ordering, but you canâ€™t change it so that â€śPlayer2â€ť appears before â€śPlayer1â€ť, because itâ€™s an unordered collection. Iâ€™m using programming terms commonly found in other programming languages. Read more here.

1 Like

This work but how can I found the player and his Value for the leaderboards textlabels ?

You need to index the array elements:

``````for _, playerData in ipairs(sortedTable)
-- Of course you'd have your own way of getting the labels here
local nameTextLabel: TextLabel, pointsTextLabel: TextLabel

nameTextLabel.Text = playerData.Key
pointsTextLabel.Text = playerData.Val
end
``````

You can change the â€śKeyâ€ť and â€śValâ€ť throughout your code to be something like â€śNameâ€ť and â€śPointsâ€ť - this will make reading it much easier, but it comes at the cost of restricting the function to the specific script (you can still use it of course, but it wouldnâ€™t make sense).

Ok thank you very much , I was stuck with this since yersteday

1 Like

Youâ€™re welcome! Please mark my post as a solution so other people can find the answer quickly (this is a rather common problem). Good luck with the game!

I found the problem, when using table.sort on dictionaries, it sorts the keys (Player1, Player2, Player3, Player4) and not the values (3, 1, 10, 5)

``````local TheTable = {
Player1 = 3,
Player2 = 1,
Player3 = 10,
Player4 = 5,
}

function sortTable()
local noKeys = {}
for player, value in TheTable do
local playerNum = string.gsub(player, "%a", "")
table.insert(noKeys, string.format("%dP%f", value, playerNum)
end
table.sort(noKeys, function(a, b)
return a > b
end)
for player, _ in TheTable do
local value
for _, v in noKeys do
local playerNum = string.split(v, "P")[3]
if "Player"..playerNum == player then
value = string.split(v, "P")[1]
break
end
end
player = value
end
noKeys = nil
return TheTable
end
``````

edit: I post this and see a solution that spans just a few lines (oof)

Thank you for your answer too, some people will maybe use your issue. Thank you very much too

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.