Table.sort error

Hey, I’m having problems sorting a list of items by their price and price currency.

local WeaponList = Weapons:GetChildren()
--sort weapon list
table.sort(WeaponList,function(a,b)
	if a.Values.PriceCurrency.Value == "Coins" and b.Values.PriceCurrency.Value == "Gems" then
		return true
	else
		return (a.Values.Price.Value < b.Values.Price.Value)
	end
end)

But lua gives me this error: invalid order function for sorting
Not sure why/what this means…?

1 Like
local WeaponList = Weapons:GetChildren()
--sort weapon list
table.sort(WeaponList,function(a,b)
	if a.Values.PriceCurrency.Value == "Coins" and b.Values.PriceCurrency.Value == "Gems" then
		return true
	elseif a.Values.PriceCurrency.Value == "Gems" and b.Values.PriceCurrency.Value == "Coins" then
                return false
        else
		return (a.Values.Price.Value < b.Values.Price.Value)
	end
end)

quick fix. Not sure how your data structure looks exactly, so I can’t thoroughly help you.

3 Likes

Ya that worked thanks

table.sort can compare values that are going to end up in random places of the result table. a can be coins, b can be coins, they can be both gems, both different, different but switched up. I guess your sort function just got confused where you gave it something like x > a, b < a and b > x (which would be invalid). I’m not 100% sure how it works but it probably works like that XD. At least I work with it assuming it works like that and I do some complex sorting crap.

1 Like