 # Table sort by time.

Hi,

I am currently working on a team balancing system for my game mode and can’t seem to figure out how to make it move the most recent player added to a team. Currently it moves the one with the highest score.

``````
SetGlobalInt( "Unbalanced", 0 )
local teamPrevent = 0
if CurTime() > teamPrevent then
teamPrevent = CurTime() + 4
local amtB = #team.GetPlayers( 2 )
local amtR = #team.GetPlayers( 1 )
if amtB - amtR >= 1 and amtB + amtR == 2 then
if GetGlobalInt( "Unbalanced" ) ~= 2 then
SetGlobalInt( "Unbalanced", 2 )
end
elseif amtB - amtR >= 2 then
if GetGlobalInt( "Unbalanced" ) ~= 2 then
SetGlobalInt( "Unbalanced", 2 )
end
elseif amtR - amtB >= 1 and amtB + amtR == 2 then
if GetGlobalInt( "Unbalanced" ) ~= 1 then
SetGlobalInt( "Unbalanced", 1 )
end
elseif amtR - amtB >= 2 then
if GetGlobalInt( "Unbalanced" ) ~= 1 then
SetGlobalInt( "Unbalanced", 1 )
end
elseif amtB - amtR < 2 and amtR - amtB < 2 then
if GetGlobalInt( "Unbalanced" ) ~= 0 then
SetGlobalInt( "Unbalanced", 0 )
end
end
end
end )

function team.GetSortedPlayers( tea )
local tab = team.GetPlayers( tea )
table.sort( tab, function( a, b ) return a:Frags() > b:Frags() end )
return tab
end

timer.Create( "TeamBalance", 10, 0, function()
if GetGlobalInt( "Unbalanced" ) > 0 then
local more
local less
if GetGlobalInt( "Unbalanced" ) == 1 then
more = 1
less = 2
elseif GetGlobalInt( "Unbalanced" ) == 2 then
more = 2
less = 1
end
local tmore = team.GetSortedPlayers( more )
local tless = team.GetSortedPlayers( less )
local diff = math.floor( ( #tmore - #tless ) / 2 )
local temp = {}
for i = 1, diff do
local ply = tmore[ #tmore - ( i - 1 ) ]
ply:ConCommand( "tdm_setteam " ..  less )
table.insert( temp, ply:Nick() )
end
local exp = table.concat( temp, ", " )
ULib.tsayColor( nil, false,Color( 255, 0, 0 ), "[",Color( 150, 150, 150 ), "TeamBalance", Color( 200, 0, 0), "] ", Color( 255, 255, 255 ), "Player(s) " .. exp .. " were switched" )
end
end )

``````