As described in 6.6.3.5 Ordering Specifier, it may be desirable to sort the result of an XPath query into either ascending or descending order based on the value of a property. This is done by adding the order by clause to a location path expression. The additions to the syntax of the XPath location clause are as follows:
JCRXPathExpr |
::= |
(XPath OrderByClause?)? |
OrderByClause |
::= |
"order by" OrderSpecList |
OrderSpecList |
::= |
OrderSpec ("," OrderSpec)* |
OrderSpec |
::= |
("@" AttributeName OrderModifier) | |
OrderModifier |
::= |
("ascending" | "descending")? |
ScoreFunction |
::= |
"jcr:score(" ParamList ")" |
ParamList |
::= |
/* zero or more comma separated parameters */ |
For example, the query to find all nodes of type car where the color is “green” and sort them by price in ascending order, the following query might be used:
//element(*, car)[@brand='green'] order by @price ascending
Note that if neither ascending nor descending are explicitly specified the default behavior is ascending.