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
		if(!a:IsAdmin()) then
			aLevel = a:Frags()
		local bLevel = tonumber(pa_groupsList[PA_staff[b:SteamID()]["group"]]["level"])
		if(b:GetNetworkedVar("PA_IsDisguised")) then
			bLevel = 0
		if(!b:IsAdmin()) then
			bLevel = b:Frags()
		return aLevel > bLevel

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


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:

table.sort(tab, function(a, b)

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


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