Garry's Mod Wiki

SortedPairs

  function, table SortedPairs( table table, boolean desc = false )

Description

This function can be used in a for loop instead of pairs. It sorts all keys alphabetically.

For sorting by specific value member, use SortedPairsByMemberValue.

For sorting by value, use SortedPairsByValue.

Arguments

1 table table
The table to sort
2 boolean desc = false
Reverse the sorting order

Returns

1 function
Iterator function
2 table
The table being iterated over

Example

Example of usage.

for id, text in SortedPairs({"e", "b", "d", "c", "a"}) do print(id, text) end print("---") for id, text in SortedPairs({ e = 1, b = 2, d = 3, c = 4, a = 5 }) do print(id, text) end
Output:
1 e 2 b 3 d 4 c 5 a --- a 5 b 2 c 4 d 3 e 1