Sorting leaderboard



function ENT:Draw()
		
	cam.Start3D2D(self:GetPos() - self:GetAngles():Forward() * 190 + self:GetAngles():Right() * 190, self:GetAngles() + Angle(90, 90, 90), 0.3)
			
		draw.RoundedBox(3, 0, 0, 1270, 50, Color( 100, 0, 0, 255 ))
		draw.RoundedBox(3, 0, 70, 1270, 50, Color( 100, 0, 0, 255 ))
		draw.DrawText(Hostname, "HostFont", 5, 0, Color( 255, 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
		draw.DrawText("Name                                                   Kills       Rounds      Won", "HostFont", 5, 70, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			
		for k,v in pairs(player.GetAll()) do
		
			

			draw.RoundedBox(3, 0, Slots[k], 675, 50, Color( 204, 0, 0, 255 ))
			draw.RoundedBox(3, 680, Slots[k], 195, 50, Color( 204, 0, 0, 255 ))
			draw.RoundedBox(3, 885, Slots[k], 195, 50, Color( 204, 0, 0, 255 ))
			draw.RoundedBox(3, 1090, Slots[k], 180, 50, Color( 204, 0, 0, 255 ))
				
			draw.DrawText(v:Name(), "PlayerFont", 5, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			draw.DrawText(v:GetNetworkedInt( "GameKillsInt", 0 ), "PlayerFont", 760, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			draw.DrawText(v:GetNetworkedInt( "GameRoundsInt", 0 ), "PlayerFont", 960, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			draw.DrawText(v:GetNetworkedInt( "GameRoundsWonInt", 0 ), "PlayerFont", 1160, Slots[k], Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			
				
		end
			
	cam.End3D2D()
	

end


I am looking to sort players on this leaderboard from the highest NetworkedInt to lowest, the NWInts are



	   v:SetNWInt("GameRoundsInt", v:GetPData("GameRounds"))
	   v:SetNWInt("GameRoundsWonInt", v:GetPData("GameRoundsWon"))
	   v:SetNWInt("GameKillsInt", v:GetPData("GameKills"))


I can’t figure it out, any help would be appreciated.

You can loop through the table, or use table.sort

I have



local PLAYERS = player.GetAll()

table.sort( PLAYERS, function( a, b ) return a:GetNWInt( "GameKills" ) > b:GetNWInt( "GameKills" ) end )


How can I implement this though? Sorry for my ignorance on this subject.

Just loop through the PLAYERS table rather than the player.GetAll() table

Like



local SortTable = table.sort( PLAYERS, function( a, b ) return a:GetNWInt( "GameKills" ) > b:GetNWInt( "GameKills" ) end )

for k,v in pairs(SortTable) do


this? obviously not right but I don’t know much about tables.

:snip: didn’t work

Yeah, that didn’t work. Is there any other way I could sort it? Guessing not

Perhaps

ipairs would work

[editline]25th April 2016[/editline]

Actually, it probably won’t, ignore me

It isn’t working because you are getting the wrong NWInt: You are trying to get the NWInt called GameKills, but only the NWInt GameKillsInt exists, as you have shown in the OP. GameKills is the PData ID while GameKillsInt is the NWInt ID, don’t mix them up.
As such, MPan1’s solution was correct, but the wrong value was being retrieved (and so, it defaulted to 0 since the NWInt was not found); here’s what you should have:

[lua]
local PLAYERS = player.GetAll()

table.sort( PLAYERS, function( a, b ) return a:GetNWInt( “GameKillsInt” ) > b:GetNWInt( “GameKillsInt” ) end )

for k,v in pairs(PLAYERS) do
[/lua]

This makes no players called at all, just blank. Odd.