Garry's Mod Wiki

table.SortByKey

  table table.SortByKey( table tab, boolean descending = false )

Description

Returns a list of keys sorted based on values of those keys.

For normal sorting see table.sort.

Arguments

1 table tab
Table to sort. All values of this table must be of same type.
2 boolean descending = false
Should the order be descending?

Returns

1 table
A table of keys sorted by values from supplied table.

Example

Example usage of the function.

local t = {} t['h'] = 2 -- Lowest value t['a'] = 150 -- Highest value t['x'] = 30 PrintTable( table.SortByKey( t ) ) PrintTable( table.SortByKey( t , true ) )
Output:
1 = a 2 = x 3 = h
1 = h 2 = x 3 = a