Sorting a table by two variables

I made a custom scoreboard and on it I want to sort players by the staff rank and their frags. How do I do it?

I came up with this:



table.sort(player.GetAll(), function(a, b)
		local aLevel = tonumber(pa_groupsList[PA_staff[a:SteamID()]["group"]]["level"])
		if(a:GetNetworkedVar("PA_IsDisguised")) then
			aLevel = 0
		end
		if(!a:IsAdmin()) then
			aLevel = a:Frags()
		end
		
		local bLevel = tonumber(pa_groupsList[PA_staff[b:SteamID()]["group"]]["level"])
		if(b:GetNetworkedVar("PA_IsDisguised")) then
			bLevel = 0
		end
		if(!b:IsAdmin()) then
			bLevel = b:Frags()
		end
		
		return aLevel > bLevel
	end)


But this proved to be obsolete because when people with higher frags then the staff’s rank level they would go higher then them.

But I want staff to ALWAYS be on top of other players, not matter of their kills

How??!!

Don’t use player.GetAll directly.

There are several areas that would cause lots of performance issues. First, you want to cache any value you try to get from the player.
I recommend looping the players and creating a temporary table for all the values you wish to sort.

To sort using multiple ‘columns’, you want to linearly check each column you wish to sort from first to last. Something like so:

[lua]
table.sort(tab, function(a, b)

if a.val2 ~= b.val2 then return a.val2 > b.val2; end
return a.val > b.val;

end);
[/lua]

That would sort a table with val2 first (descending), then val1;