6.6.3.5 Ordering Specifier

This part of the query statement specifies the ordering of the result nodes according to the natural ordering of the values of one or more properties of the result nodes. If no order specification is supplied in the query statement, implementations may support document order on the result nodes (see 6.6.4.2 Document Order).

In both XPath and SQL, ordering is specified by a special clause that lists one or more property names and, for each, whether the order is to be ascending or descending. If neither ascending nor descending is specified after a property name (or jcr:score(...) function), the default is ascending.

Implementations must support ordering on jcr:score (in SQL just the name is used, see 8.5.2.4 Pseudo-property) or jcr:score(...) (in XPath, the function form is used, though the number of parameters depends on the implementation, see 6.6.5.3 jcr:score function).

Support for ordering on PATH and NAME properties is not required. If it is supported then the collation sequence for these types is implementation specific.

XPath: A subset of the order by clause specified in XQuery 1.0 is used to implement the order specification in XPath. If the order modifier (ascending or descending) is missing, the ordering defaults to ascending. See 6.6.5.5 order by Clause.

SQL:The order by clause implements the order specification in SQL. If the order modifier (ASC meaning ascending or DESC, meaning descending) is missing, the ordering defaults to ASC. See 8.5.4.6 ORDER BY.

Examples:

SQL

XPath

SELECT *
FROM my:type
ORDER BY my:title

//element(*, my:type)
order by @my:title

SELECT *
FROM my:type
ORDER BY my:date DESC, my:title ASC

//element(*, my:type)
order by @my:date descending, @my:title ascending

SELECT *
FROM my:type
WHERE
CONTAINS(*, 'jcr') ORDER BY jcr:score DESC

//element(*, my:type)
[jcr:contains(., 'jcr')]
order by jcr:score() descending