Module jakarta.data
Package jakarta.data

Record Class Sort<T>

java.lang.Object
java.lang.Record
jakarta.data.Sort<T>
Type Parameters:
T - entity class of the property upon which to sort.
Record Components:
property - name of the property to order by.
isAscending - whether ordering for this property is ascending (true) or descending (false).
ignoreCase - whether or not to request case insensitive ordering from a database with case sensitive collation.

public record Sort<T>(String property, boolean isAscending, boolean ignoreCase) extends Record

Requests sorting on a given entity attribute.

An instance of Sort specifies a sorting criterion based on an entity field, with a sorting direction and well-defined case sensitivity.

A query method of a repository may have a parameter or parameters of type Sort if its return type indicates that it may return multiple entities. Parameters of type Sort must occur after the method parameters representing regular parameters of the query itself.

Alternatively, dynamic Sort criteria may be specified when requesting a page of results.

The parameter type Sort<?>... allows a variable number of generic Sort criteria. For example,

 Employee[] findByYearHired(int yearYired, Limit maxResults, Sort<?>... sortBy);
 ...
 highestPaidNewHires = employees.findByYearHired(Year.now(),
                                                 Limit.of(10),
                                                 Sort.desc("salary"),
                                                 Sort.asc("lastName"),
                                                 Sort.asc("firstName"));
 

Alternatively, Order may be used in combination with the static metamodel to allow a variable number of typed Sort criteria. For example,

 Employee[] findByYearHired(int yearYired, Limit maxResults, Order<Employee> sortBy);
 ...
 highestPaidNewHires = employees.findByYearHired(Year.now(),
                                                 Limit.of(10),
                                                 Order.by(_Employee.salary.desc(),
                                                          _Employee.lastName.asc(),
                                                          _Employee.firstName.asc()));
 

When multiple sorting criteria are provided, sorting is lexicographic, with the precedence of a criterion depending on its position with the list of criteria.

A repository method may declare static sorting criteria using the (OrderBy keyword or OrderBy annotation, or using Query with an ORDER BY clause), and also accept dynamic sorting criteria via its parameters. In this situation, the static sorting criteria are applied first, followed by any dynamic sorting criteria specified by instances of Sort .

In the example above, the matching employees are sorted first by salary from highest to lowest. Employees with the same salary are then sorted alphabetically by last name. Employees with the same salary and last name are then sorted alphabetically by first name.

A repository method throws IllegalArgumentException if it is called with an argument or arguments of type Sort and a separate argument of type PageRequest with nonempty sort criteria.

A repository method throws DataException if the database is incapable of ordering the query results using the given sort criteria.