table.sort
From GMod Wiki
| 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: |
|
| BBCode Link: | [b][url=http://wiki.garrysmod.com/?title=Table.sort]Table.sort [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] |
Example
| Description | Sort the values of a Table . |
|---|---|
| Used on | |
| 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 |
| Output | Prints a Table in alphabetical and random order into the console. |