- 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.
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.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Sort
<T> Create aSort
instance withascending direction
that does not request case insensitive ordering.static <T> Sort
<T> ascIgnoreCase
(String property) Create aSort
instance withascending direction
and case insensitive ordering.static <T> Sort
<T> Create aSort
instance withdescending direction
that does not request case insensitive ordering.static <T> Sort
<T> descIgnoreCase
(String property) Create aSort
instance withdescending direction
and case insensitive ordering.final boolean
Indicates whether some other object is "equal to" this one.final int
hashCode()
Returns a hash code value for this object.boolean
Indicates whether or not to request case insensitive ordering from a database with case sensitive collation.boolean
Indicates whether to sort the property in ascending order (true) or descending order (false).boolean
Indicates whether to sort the property in descending order (true) or ascending order (false).static <T> Sort
<T> Create aSort
instanceproperty()
Name of the property to order by.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
Sort
Defines sort criteria for an entity property. For more descriptive code, use:
Sort.asc(propertyName)
for ascending sort on a property.Sort.ascIgnoreCase(propertyName)
for case insensitive ascending sort on a property.Sort.desc(propertyName)
for descending sort on a property.Sort.descIgnoreCase(propertyName)
for case insensitive descending sort on a property.
- Parameters:
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.
-
-
Method Details
-
property
Name of the property to order by.- Returns:
- The property name to order by; will never be
null
.
-
ignoreCase
public boolean ignoreCase()Indicates whether or not to request case insensitive ordering from a database with case sensitive collation. A database with case insensitive collation performs case insensitive ordering regardless of the requested
ignoreCase
value.- Returns:
- Returns whether or not to request case insensitive sorting for the property.
-
isAscending
public boolean isAscending()Indicates whether to sort the property in ascending order (true) or descending order (false).- Returns:
- Returns whether sorting for this property shall be ascending.
-
isDescending
public boolean isDescending()Indicates whether to sort the property in descending order (true) or ascending order (false).- Returns:
- Returns whether sorting for this property shall be descending.
-
of
Create aSort
instance- Type Parameters:
T
- entity class of the sortable property.- Parameters:
property
- the property name to order bydirection
- the direction in which to order.ignoreCase
- whether to request a case insensitive ordering.- Returns:
- a
Sort
instance. Nevernull
. - Throws:
NullPointerException
- when there is a null parameter
-
asc
Create aSort
instance withascending direction
that does not request case insensitive ordering.- Type Parameters:
T
- entity class of the sortable property.- Parameters:
property
- the property name to order by- Returns:
- a
Sort
instance. Nevernull
. - Throws:
NullPointerException
- when the property is null
-
ascIgnoreCase
Create aSort
instance withascending direction
and case insensitive ordering.- Type Parameters:
T
- entity class of the sortable property.- Parameters:
property
- the property name to order by.- Returns:
- a
Sort
instance. Nevernull
. - Throws:
NullPointerException
- when the property is null.
-
desc
Create aSort
instance withdescending direction
that does not request case insensitive ordering.- Type Parameters:
T
- entity class of the sortable property.- Parameters:
property
- the property name to order by- Returns:
- a
Sort
instance. Nevernull
. - Throws:
NullPointerException
- when the property is null
-
descIgnoreCase
Create aSort
instance withdescending direction
and case insensitive ordering.- Type Parameters:
T
- entity class of the sortable property.- Parameters:
property
- the property name to order by.- Returns:
- a
Sort
instance. Nevernull
. - Throws:
NullPointerException
- when the property is null.
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object)
; primitive components are compared with '=='.
-