Table.sort not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want the rank table to be sorted so the with the highest requiredpoints comes first.

  2. What is the issue?
    It does not sort it and it stays the same.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I looked on google and tried some solution none worked.
I also tried to do Ranks = table.sort(Ranks,Sort)

local Ranks = {
	Newbie = {
		RequiredPoints = 0,
		Color = Color3.fromRGB(39, 255, 140)
	},
	Pro = {
		RequiredPoints = 100,
		Color = Color3.fromRGB(39, 255, 140)
	},
	God = {
		RequiredPoints = 5000,
		Color = Color3.fromRGB(39, 255, 140)
	},
}
		local function Sort (a,b)
			return a.RequiredPoints < b.RequiredPoints
		end
		table.sort(Ranks,Sort)
print(Ranks)

Result:

{
                    ["God"] =  ▼  {
                       ["Color"] = 0.152941, 1, 0.54902,
                       ["RequiredPoints"] = 5000
                    },
                    ["Newbie"] =  ▼  {
                       ["Color"] = 0.152941, 1, 0.54902,
                       ["RequiredPoints"] = 0
                    },
                    ["Pro"] =  ▼  {
                       ["Color"] = 0.152941, 1, 0.54902,
                       ["RequiredPoints"] = 100
                    }
                 }
2 Likes

Unfortunately, dictionaries (where the indexes of the table are anything but integral/numbers) cannot be sorted.

An alternative is to either write a function to copy all of the dictionary’s contents as indexes instead, or to make the name of the rank an additional field of the table object itself, which may ruin convenience a tad.

First option if you’re interested in that:

local function copyDictionaryAsTable()
local tab = {}

for rankName, rankData in pairs(Ranks) do
rankData.Rank = rankName --//Add reference to whatever the rank name is.
table.insert(tab, rankData)
end

return tab
end

local sortedRanks = copyDictionaryAsTable()
--//run sorting algorithm now
2 Likes
local Ranks = {
	{
		Name = "Newbie"
		RequiredPoints = 0,
		Color = Color3.fromRGB(39, 255, 140)
	},
	{
		Name = "Pro"
		RequiredPoints = 100,
		Color = Color3.fromRGB(39, 255, 140)
	},
	{
		Name = "God"
		RequiredPoints = 5000,
		Color = Color3.fromRGB(39, 255, 140)
	},
}

for i, rank in ipairs(Ranks) do
	print(rank.Name, rank.RequiredPoints, rank.Color)
end

if you create your table like this then you can loop the ranks in order without the need to sort them

2 Likes

Right, but I wasn’t sure if he wanted to keep it as a dictionary.

1 Like

Yer me 2 but if he wants a dictionary then he could just do

local dictionary = {}
for i, rank in ipairs(Ranks) do
	dictionary[rank.Name] = rank
end
1 Like

The order is not guaranteed, which takes us back to the original issue.

Yer also the words newbie pro god are kinda useless as keys he would be better just using the keys 1 2 and 3 from the normal array and just save the players rank as numbers in the datastore if that’s what he is doing

1 Like