XSLT Reference
array:sort()
Returns a new array with members sorted using an optional collation and key function.
array:sort(array, collation?, key-function?)Description
array:sort() returns a new array whose members are in ascending order, determined by the sort key and collation. When no key function is supplied, members are compared directly using the default collation for strings or natural ordering for numbers and other atomic types. When a key function is supplied, it is applied to each member to derive the sort key; members are then sorted by their keys.
This function is the array equivalent of sort() for sequences or xsl:sort in templates. It does not modify the input array; it always returns a new one.
The collation argument controls string comparison. The key-function takes a single argument (the array member, which is a sequence) and returns an atomic value to use as the sort key.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
array | array(*) | Yes | The input array to sort. |
collation | xs:string? | No | URI of the collation to use for string comparison. Empty sequence uses the default. |
key-function | function(item()) as xs:anyAtomicType | No | A function that extracts the sort key from each member. |
Return value
array(*) — a new array with the same members in sorted order.
Examples
Sorting numbers in ascending order
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:array="http://www.w3.org/2005/xpath-functions/array">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="nums" select="[5, 2, 8, 1, 9, 3]"/>
<xsl:variable name="sorted" select="array:sort($nums)"/>
<xsl:value-of select="array:flatten($sorted)" separator=" "/>
</xsl:template>
</xsl:stylesheet>
Output:
1 2 3 5 8 9
Sorting records by a key field
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<data/>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:map="http://www.w3.org/2005/xpath-functions/map">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/data">
<xsl:variable name="people" select="[
map{'name': 'Charlie', 'age': 35},
map{'name': 'Alice', 'age': 28},
map{'name': 'Bob', 'age': 42}
]"/>
<xsl:variable name="by-name" select="array:sort($people, (), function($p) { map:get($p, 'name') })"/>
<sorted>
<xsl:for-each select="1 to array:size($by-name)">
<xsl:variable name="p" select="array:get($by-name, .)"/>
<person name="{map:get($p, 'name')}" age="{map:get($p, 'age')}"/>
</xsl:for-each>
</sorted>
</xsl:template>
</xsl:stylesheet>
Output:
<sorted>
<person name="Alice" age="28"/>
<person name="Bob" age="42"/>
<person name="Charlie" age="35"/>
</sorted>
Notes
array:sort()always sorts in ascending order. To sort in descending order, reverse the result witharray:reverse().- The collation argument may be
()(empty sequence) to use the default collation, allowing the key function to be specified without supplying a collation. - Members that are sequences are compared by their atomized value; members that cannot be compared raise a type error.
- An empty array returns an empty array.