Garry's Mod Wiki

SortedPairsByMemberValue

  function, table SortedPairsByMemberValue( table table, any memberKey, boolean descending = false )

Description

Returns an iterator function that can be used to loop through a table in order of member values, when the values of the table are also tables and contain that member.

To sort by value, use SortedPairsByValue.

To sort by keys, use SortedPairs.

Arguments

1 table table
Table to create iterator for.
2 any memberKey
Key of the value member to sort by.
3 boolean descending = false
Whether the iterator should iterate in descending order or not.

Returns

1 function
Iterator function
2 table
The table the iterator was created for.

Example

Creates a table and prints its contents in order of the age member descending

local tab = { { Name = "Adam", Age = 16 }, { Name = "Charles", Age = 18 } } for k, v in SortedPairsByMemberValue(tab, "Age", true) do print(v.Name) end
Output:
Charles Adam