table.sort

From Garry's Mod
Revision as of 12:50, 4 June 2017 by Donkie (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
 table.sort( )

Contents

Description

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

Arguments

table tbl

The table to sort.

Arguments

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.

Examples

Example

Sorting table by an integer

local TABLE = {
	{"Jeff",8},
	{"Peter",17},
	{"Shay",11},
	{"Janine",1}
}

table.sort(TABLE,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)

Examples

Example

Sorting a player table by a NWInt

local PLAYERS = player.GetAll()

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

Output:

Player table sorted by score going from highest to lowest
Personal tools
Navigation