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.
The parameter type Sort<?>... allows a variable number
of generic Sort criteria. For example,
Employee[] findByYearHired(int yearHired, Limit maxResults, Sort<?>... sortBy);
...
highestPaidNewHires = employees.findByYearHired(Year.now().getValue(),
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 yearHired, Limit maxResults, Order<Employee> sortBy);
...
highestPaidNewHires = employees.findByYearHired(Year.now().getValue(),
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,
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 jakarta.
if the database is incapable of ordering the query results using the given
sort criteria.
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | ignoreCase
Record Component accessed by ignoreCase(). whether or not to request case insensitive ordering from a database with case sensitive collation. |
| private final boolean | isAscending
Record Component accessed by isAscending(). whether ordering for this property is ascending (true) or descending (false). |
| private final String | property
Record Component accessed by property(). name of the property to order by. |
| Access | Constructor and Description |
|---|---|
| public | Sort(String
name of the property to order by. property, boolean whether ordering for this property is ascending (true) or descending (false). isAscending, boolean whether or not to request case insensitive ordering from a database with case sensitive collation. ignoreCase)Defines sort criteria for an entity property. |
| Modifier and Type | Method and Description |
|---|---|
| public static < entity class of the sortable property. T> Sort | asc(String
the property name to order by property)Create a |
| public static < entity class of the sortable property. T> Sort | ascIgnoreCase(String
the property name to order by. property)Create a |
| public static < entity class of the sortable property. T> Sort | desc(String
the property name to order by property)Create a |
| public static < entity class of the sortable property. T> Sort | descIgnoreCase(String
the property name to order by. property)Create a |
| public final boolean | equals(Object
the reference object with which to compare. o)Implements abstract java. Indicates whether some other object is "equal to" this one. |
| public final int | |
| public boolean | Returns: Returns whether or not to request case insensitive sorting for the property.Record Component getter of ignoreCase. Indicates whether or not to request case insensitive ordering from a database with case sensitive collation. Record Component whether or not to request case insensitive ordering from a database with case sensitive collation. |
| public boolean | Returns: Returns whether sorting for this property shall be ascending.Record Component getter of isAscending. Indicates whether to sort the property in ascending order (true) or descending order (false). Record Component whether ordering for this property is ascending (true) or descending (false). |
| public boolean | Returns: Returns whether sorting for this property shall be descending.Indicates whether to sort the property in descending order (true) or ascending order (false). |
| public static < entity class of the sortable property. T> Sort | |
| public String | |
| public final String | toString()
Implements abstract java. Returns a string representation of the record. |
| ignoreCase | back to summary |
|---|---|
| private final boolean ignoreCase Record Component accessed by ignoreCase(). whether or not to request case insensitive ordering from a database with case sensitive collation. | |
| isAscending | back to summary |
|---|---|
| private final boolean isAscending Record Component accessed by isAscending(). whether ordering for this property is ascending (true) or descending (false). | |
| property | back to summary |
|---|---|
| private final String property Record Component accessed by property(). name of the property to order by. | |
| Sort | back to summary |
|---|---|
| public Sort(String property, boolean isAscending, boolean ignoreCase) Defines sort criteria for an entity property. For more descriptive code, use:
| |
| asc | back to summary |
|---|---|
| public static <T> Sort Create a
| |
| ascIgnoreCase | back to summary |
|---|---|
| public static <T> Sort Create a
| |
| desc | back to summary |
|---|---|
| public static <T> Sort Create a
| |
| descIgnoreCase | back to summary |
|---|---|
| public static <T> Sort Create a
| |
| equals | back to summary |
|---|---|
| public final boolean equals(Object o) Implements abstract java. Doc from java. Indicates whether some other object is "equal to" this one. In addition
to the general contract of
R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
| |
| hashCode | back to summary |
|---|---|
| public final int hashCode() Implements abstract java. Doc from java. Returns a hash code value for the record.
Obeys the general contract of
| |
| ignoreCase | back to summary |
|---|---|
| public boolean ignoreCase() Record Component getter of 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
| |
| isAscending | back to summary |
|---|---|
| public boolean isAscending() Record Component getter of isAscending. Indicates whether to sort the property in ascending order (true) or descending order (false).
| |
| isDescending | back to summary |
|---|---|
| public boolean isDescending() Indicates whether to sort the property in descending order (true) or ascending order (false).
| |
| of | back to summary |
|---|---|
| public static <T> Sort Create a
| |
| property | back to summary |
|---|---|
| public String property() Record Component getter of property. Name of the property to order by.
| |
| toString | back to summary |
|---|---|
| public final String toString() Implements abstract java. Doc from java. Returns a string representation of the record.
In accordance with the general contract of In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.
| |