Sorting
The Query API includes a variety of sort-related methods to order search results by field. Sorted fields must be indexes.
Two common sorts are performed with the sortAscending
and sortDescending
methods. The following query sorts Author
objects in descending alphabetical order by last name:
List<Author> authors = Query.from(Author.class).sortDescending("lastName");
For spatial queries for objects containing a Location field, you can use sortClosest
or sortFarthest
. These methods sort results in closet or farthest order from a given location.
For advanced sorting, you can use the sortNewest
, sortOldest
, and sortRelevant
methods, which prioritize a specified field value with a specified weight.
All of the sort-related methods use the Sorter class. You can create a custom Sorter
class and add it to the Query
instance with the sort method.