TableSort doesn't sort

Hello, why this doesn’t work? It’s a simple leaderboard, top coins

A for loop through game.Players:

TableSort[plr.Name] = tonumber(data.Coins.value)

Sorting:

table.sort(TableSort,function(a,b)
			return a[1] > b[1]
		end
	)

Print:

for playerName, Coins in pairs(TableSort) do
		
		print(playerName..": "..Coins)
		
end
1 Like

The dictionary part of a table has no notion of order. You should be using the array part of a table if you need order.

table.insert(TableSort, { name = player.Name, amount = data.Coins.value })
table.sort(TableSort, function(a, b)
    return a.amount > b.amount
end)
1 Like

Hey thank you! and how can I see the results?

Cause this for is not working anymore

for playerName, Coins in pairs(TableSort) do
	
	print(playerName..": "..Coins)

end

Since the player name is no longer the index you do

for _, t in ipairs(TableSort) do -- ipairs guarantees order on arrays
    print(t.name, ":", t.amount)
end
1 Like

you saved my life, this post must be at least 30 characters so ye, you saved my life

2 Likes