$result := sort [ --key|:k expression ]
--compare|:c perl-code expression
$result := sort [ --key|:k expression ]
[ --numeric|:n ] [ --descending|:d ] [ --locale|:l] expression
This command sorts a given node-list, returning a node-list ordered according to a given key and ordering function.
--key|:k followed by an expression
specifies the key to be computed for each member of the
node-list and the result used as the sorting key. If omitted,
keys are created by converting the nodes
to string as if XPath expression string(.)
was used.
--numeric|:n specifies, that
keys should be compared by their numerical values
(the default is string comparison).
--descending|:d specifies,
that the result should be ordered in descending order
(default is ascending).
--locale|:l forces using
current locale settings for string comparison
(default is no locale).
--compare argument followed
by a perl-code allows to define a custom
comparison method in a similar way to Perl sort
command. The keys to be compared are passed
to the code in variables $a and
$b. The code is supposed to return 1 if
the key in $a is greater than
$b, 0 if the keys are equal
and -1 if $a
is less than $b, depending
on how the corresponding elements are to be ordered.
It is a run-time error to use
--compare together with either
--numeric or
--descending.
Example 2. Reorder creature elements by name attribute in ascending order using Czech locale settings
perl {
# setup locale collating function
# Note, that the collating function must be UTF8 aware.
use POSIX qw(locale_h);
setlocale(LC_COLLATE,'cs_CZ.UTF-8');
};
xmove &{ sort :k@name :l * } into /middle-earth[1]/creatures;
Example 3. Sort a node-list by a pre-computed score (Perl-based sort)
$results := sort --numeric --descending --key { $scores{literal('@name')} } $players;