How can I sort the table from least to greatest?

Greetings! I am very close to finishing one of the most requested systems in my game. However, I have one tiny problem.

I already know when setting a table, I can do

table.sort(PlayerRankTable, function(a, b)
	return a > b

However, that solution only works for the first part of the table. Not the second part. The first part of the table is the actual player instance. What I want to sort is the value.

Example of the table:

PlayerRankTable = {
   BillB1ox = 2,
   Player3 = 3,
   Player2 = 1,

I just want to sort those numbers.

If you can help, that would be great!

Thanks in advance!

1 Like

Well you can’t sort a dictionary so you’ll need to convert it into an array beforehand.

PlayerRankTable = {
	{"Player1", 3},
	{"Player2", 2},
	{"Player3", 1}

table.sort(PlayerRankTable, function(Left, Right)
	return Left[2] < Right[2]

for _, PlayerArray in ipairs(PlayerRankTable) do
	print(PlayerArray[1], PlayerArray[2])


1 Like