How to fix table.sort not working

I need help on a script, whenever I sort it doesnt sort correctly
hers my sorting part

script.Parent.Container.ChildAdded:Connect(function(child)
	if child:IsA("TextButton") and child.Name ~= "Template" then
		table.insert(powers,game.Players.LocalPlayer.Pets:FindFirstChild(child.Name).Value .. ":"..child.Name)
		table.sort(powers, function(a, b)
			return a > b
		end)
        print(powers[1], powers[2], powers[3], powers[4], powers[5])
	end
end)

and my output is

84:Double Small Void Star 5:Cosmic Star 5:Cosmic Star 42:Small Void Star 42:Small Void Star

(84, 5, 5, 42, 42)

even though thats not in order

@Valkyrop any help?

If those are names, maybe try -

table.sort(powers, function(a, b)
	return tonumber(a.Name) > tonumber(b.Name)
end)

Otherwise, I’d need to see more

It sort the table from highest to lowest and the highest is 84 and 2nd highest is 42 but it puts to 5s in between

It is in order. You’re comparing strings to one another, not numbers.

how could I fix this?? I dont know how

or how could i change child.Name to a number

If you absolutely need the name of the child being added and the corresponding number value, then you could do something like this.

script.Parent.Container.ChildAdded:Connect(function(child)
	if child:IsA("TextButton") and child.Name ~= "Template" then
		table.insert (powers, {game.Players.LocalPlayer.Pets:FindFirstChild(child.Name).Value, child.Name})
		
		table.sort(powers, function(a, b)
			return a[1] > b[1]
		end)
		
		print(powers[1], powers[2], powers[3], powers[4], powers[5])
	end
end)

If you only need the number, then this would work just as well

script.Parent.Container.ChildAdded:Connect(function(child)
	if child:IsA("TextButton") and child.Name ~= "Template" then
		table.insert (powers, game.Players.LocalPlayer.Pets:FindFirstChild(child.Name).Value)

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

		print(powers[1], powers[2], powers[3], powers[4], powers[5])
	end
end)
1 Like

here is a more optimal way

-- when a child is added to Container call this function
script.Parent.Container.ChildAdded:Connect(function(child)
	-- if the child is not a textbutton then exit the function
	if child:IsA("TextButton") == false then return end
	-- if the child name is template then exit the function
	if child.Name == "Template" then return end
	-- get a reference of the pet in the players pets folder
	local newPet = game.Players.LocalPlayer.Pets[child.Name]
	-- set the position to the end of the table
	local position = #powers + 1
	-- lets loop all powers to see where this new power must go
	for i, pet in powers do
		-- if the pet in the powers table has a value less then the new pet then skip to the next pet
		if pet.Value < newPet.Value then continue end
		-- set position to i
		position = i
		-- break out of this loop we no longer need to loop anymore
		break
	end
	-- add the new pet to the correct position so we don't need to sort
	table.insert(powers, position, newPet)
end