table.SortByKey

From GMod Wiki

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



Example

DescriptionSort the values of a Table by their key.
Used onNewerShared.png
Code
local notsorted = {}
notsorted[6] = "row 1"
notsorted[3] = "row 2"
notsorted[2] = "row 3"
notsorted[4] = "row 4"
notsorted[1] = "row 5"
notsorted[5] = "row 6"
local sorted = table.SortByKey(notsorted, false)
PrintTable(sorted)
-- This gets printed in the console:
-- 1 = row 5
-- 2 = row 3
-- 3 = row 2
-- 4 = row 4
-- 5 = row 6
-- 6 = row 1
 
Outputprints the Table sorted into the console console.
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox