Skip to main content

Sort array items

Anatomy

<array:sort var="var" var_result="(destructive on 'var')" type="asc" key="string"/>

Description: ARRAY:SORT sorts the items of an array in a particular order, specified by a given sorting type.

A given key may specify the column of a multidimensional array to sort by. If however, the key is omitted, then the array is treated as an unidimensional list.

Attention:

If a key is specified and that key does not exist within the inner array, the corresponding item of the outer array will be treated as NULL for the purpose of sorting.

ARRAY:SORT is destructive, in the sense that it modifies the array in-place, unless 'var_result' is specified in which case a copy of the resulting array will be stored in 'var_result' instead. It preserves the key and value associations of the array.

Attributes

NameTypeDescriptionDefined By
varvarVariable name array:sort
var_resultvarResult variable name array:sort
typetypeSorting type array:sort

Multidimensional

NameTypeDescriptionDefined By
keystringKey array:sort

Results:

BindingTypePredicate
varvar_resultarray

Examples

Example

<array var="names">
<item>Bill Gates</item>
<item>Steve Jobs</item>
<item>Larry Ellison</item>
</array>

<array:sort var="names" type="desc"/>

<output>
<array:join var="names" delimiter=", "/>
</output>

<!-- Steve Jobs, Larry Ellison, Bill Gates -->