How to sort tables with a secondary value

Currently I have a table of items that are sorted via their power:

local function Sorter(a, b)
	local Decision = a[3] > b[3]
	return Decision
end

The problem is, I want to sort it by power but if the power is == then I want it to be sorted by tier, I have an integer list assigned to each tier, example:

TierInformation.TierIntegers = {
	["Common"] = 1;
	["Rare"] = 2;
	["Ultra-Rare"] = 3;
	["Legendary"] = 4;
	["Mystical"] = 5;
	["VOID"] = 6;
}

This all works fine but when I try to implement this into the sorter function it errors, I tried doing this:

local function Sorter(a, b)
		local Decision = a[3] > b[3] or a[4] > b[4] --3 is the power, 4 is the tier integer
		return Decision
	end

If anyone has any ideas on how to sort this it would be very much appreciated! :smiley:

try doing

local function Sorter(a, b)
     if a[3] == b[3] then
          return a[4] > b[4]
     end
     return a[3] > b[3]
end

or if you wanted it in one line

local function Sorter(a, b)
    return (a[3] == b[3] and a[4] > b[4]) or (a[3] > b[3])
end

hope this helps

1 Like