table.sort

From GMod Wiki

Jump to: navigation, search
Function
Syntax table.sort( Table table [,Function comparative] )
Where is this used?
Description:
Sorts a Table into alphabetical order.
Returns: nil
Part of Library: Table
Realm: NewerShared.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=Table.sort]Table.sort [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Example

DescriptionSort the values of a Table .
Used onNewerShared.png
Code
local test_table = {"b", "a", "c", "e", "d"}
 
table.sort(test_table); // Sort the table from a to z.
PrintTable(test_table); // Print the table into our console.
 
 
// If you want to sort the table from z to a, then do this.
local test_table2 = {"b", "a", "c", "e", "d"}
 
table.sort(test_table2, function(a, b) return a > b end); // Sort our table, but this time lets make it sort backwards.
PrintTable(test_table2); // Print the table into our console.
 
 
table.sort(test_table2, function(a, b) return math.Rand( 0, 1 ) > 0.5 end); // Randomly shuffles the table.
PrintTable(test_table2); //Print it
OutputPrints a Table in alphabetical and random order into the console.


See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox