Ordering Query Results

An application can specify an order for the query result by providing an ordering statement, specified by a String that contains one or more ordering declarations, separated by commas. Each ordering declaration is a Java expression of an orderable type, followed by either ascending or descending . Your ordering expression may use the . operator to navigate references.

Each ordering expression must be one of the following types:

  • Any primitive type except boolean

  • Any wrapper type except Boolean

  • BigDecimal

  • BigInteger

  • String

  • Date

We mentioned earlier that JDO does not define the ordering of Strings when you use the comparison operators (<, <=, >, and >=). This also applies for the ordering of query results.

The following Query method binds the ordering statement to the Query instance:

void setOrdering(String ordering);

The ordering statement may include multiple ordering expressions. The result of the leftmost expression is used first to order the results. If the leftmost expression evaluates to the same value for two or more elements, then the second expression is used to order those elements. If the second expression also evaluates to the same value, then the third expression is used, and so on, until the last expression is evaluated. If the values of all of the ordering expressions are equal for two or more elements, then the ordering of those elements is unspecified.

The following example demonstrates the use of ordering:

public static void queryTransactionsInCity(PersistenceManager ...

Get Java Data Objects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.