Cayenne User Documentation
Using Orderings

To sort SelectQuery results, orderings are used. Orderings use path expressions discussed in the previous section to identify the attributes that must be used in sorting. For example to order results by artist name, the following code can be used:

    import org.apache.cayenne.query.SelectQuery;
    ...
    SelectQuery query = new SelectQuery("Artist");

    // add ordering by Artist name:
    query.addOrdering("artistName", true);

Orderings also support in-memory sorting of lists of Java Beans (all DataObjects are normally Java Beans, since they has set/get method pairs for all the properties). For instance to sort with a single ordering, the following code might be used:

    import org.apache.cayenne.query.Ordering;
    ...
    // assume this is a properly initialized list of Artists
    List list = ...;

    // creates asending ordering by Artist name
    Ordering ordering = new Ordering("artistName", true);

    // orders a list
    ordering.orderList(list);

If there is a need to sort on more than one object property, multiple Orderings can be passed as a List to a static method `orderList(List, List)`. The cost of adding new Orderings decreases, as the list of objects ends up being sorted by the first Ordering, then, if any two objects are equal for first Ordering, by the second, and so on.

    import org.apache.cayenne.query.Ordering;
    ...
    // assume this is a properly initialized list of Paintings
    List list = ...;

    List orderings = new ArrayList();
    orderings.add(new Ordering("paintingTitle", true));
    orderings.add(new Ordering("estimatedPrice", false));

    // orders a list aplying multiple orderings
    Ordering.orderList(list, orderings);

.
Copyright ©2001-2007 Apache Software Foundation