SortedPairsByMemberValue

From Garry's Mod
Jump to: navigation, search
 SortedPairsByMemberValue( )

Contents

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

table table

Table to create iterator for.

Arguments

any memberKey

Key of the value member to sort by.

Arguments

boolean descending=false

Whether the iterator should iterate in descending order or not.

Returns

function

Iterator function

Returns

table

The table the iterator was created for.

Examples

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
Personal tools
Navigation