Garry's Mod Wiki


  table.sort( table tbl, function sorter )


Sorts a sequential table either ascending or by the given sort function.

This function modifies the table you give to it.


1 table tbl
The table to sort.
2 function sorter
If specified, the function will be called with 2 parameters each. Return true in this function if you want the first parameter to come first in the sorted array.


Sorting table by an integer

local tbl = { { "Jeff", 8 }, { "Peter", 17 }, { "Shay", 11 }, { "Janine", 1 } } table.sort( tbl, function(a, b) return a[2] > b[2] end )
Output: Table going from highest number to lowest (1: Peter, 2: Shay, 3: Jeff, 4: Janine)


Sorting a player table by a NWInt

local plys = player.GetAll() table.sort( plys, function(a, b) return a:GetNWInt("Score") > b:GetNWInt("Score") end )
Output: Player table sorted by score going from highest to lowest