Unfortunately, there’s no easy way around this. Dictionaries, unlike arrays, cannot have an order.
Solution 1 Convert to Array
You’ll need to convert it to arrays like this.
local players = {
{'coolboy123', 216},
{'funfungamers', 683},
{'asdasdasd', 120}
}
And then sort the table with the following code:
table.sort(players, function(a, b)
return a[2] > b[2]
end)
Our table is now:
funfungamers 683
coolboy123 216
asdasdasd 120
Solution 2 spairs
Credit to this StackOverflow post
Here’s a modified version of pairs- called spairs
(This is in a modulescript)
return function (t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
Here’s an example use:
local spairs = require(script.spairs)
local plrs = {}
plrs['coolboy123'] = 216
plrs['funfungamers'] = 683
plrs['asdasdasd'] = 120
local function order(t, a, b)
return t[b] < t[a]
end
for i,v in spairs(plrs, order) do
print(i, v)
end
Our output is as expected.
funfungamers 683
coolboy123 216
asdasdasd 120