# saxon:sort()

The `saxon:sort` function is provided primarily for use in XPath, which has no built-in sorting capability. In XSLT it is preferable to use `xsl:sort`, in XQuery to use a FLWOR expression with an `order by` clause.

saxon:sort($seq as item()*) => item()*

This form of the function sorts a sequence of nodes and/or atomic values. For atomic values, the value itself is used as the sort key. For nodes, the atomized value is used as the sort key. The atomized value must be a single atomic value. The values must all be comparable. Strings are sorted using codepoint collation.

saxon:sort($seq as item()*, $stored-expression as jt:net.sf.saxon.functions.Evaluate-PreparedExpression) => node()?

This form of the function sorts a sequence of nodes and/or atomic values, using the supplied stored expression to compute the sort key for each item in the sequence. The computed sort key must either be a single atomic value, or a node that atomizes to a single atomic value, and the sort keys must all be comparable. Strings are sorted using codepoint collation.

A stored expression may be obtained as the result of calling the saxon:expression function.

The stored expression is evaluated for each item in $seq in turn, with that item as the context node, with the context position equal to the position of that item in $seq, and with the context size equal to the size of $seq.

Example: saxon:sort(sale, saxon:expression('@price * @qty')) will evaluate price times quantity for each child <sale> element, and return the `sale` elements in ascending numeric order of this value.

Next