Am i using table.sort wrong?

I’m currently trying to sort a dictionary table that’s meant to look something like this:

table = {
["SwordName"] = {Price, Name}
}

I’m trying to sort the table by its price and this is my code for doing this:

local T = {}

for i, v in pairs(game.ReplicatedStorage.Swords:GetChildren()) do
	table.insert(T, i, v.Name == {v.Price.Value, v.Name})
end

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

for i, v in pairs(T) do
	print(v[1])
end

In the console it prints that at the print(v[1]) i’m attempting to index a boolean with a number. What am i doing wrong here?

this piece of code is literally saving booleans. Your table is full of booleans, and you can’t compare booleans like this

Since they’re not numbers.

Try changing

table.insert(T, i, v.Name == {v.Price.Value, v.Name})

to

table.insert(T, i, v.Name = {v.Price.Value, v.Name})

Why are you comparing instead of defining in your first loop. You cant compare if there is no if statement. So change the = to =.

When doing v[1] its probably trying to index a bool value.

This is a syntax error because = assignments can’t be used in expressions. What OP wants is probably table.insert(T, {v.Price.Value, v.Name}). Also, the i parameter is optional, and just inserts at the end of the table.

3 Likes