- Type Parameters:
T- entity class of the entity attribute upon which to sort.- Record Components:
property- name of the entity attribute to order by.isAscending- whether ordering for this attribute 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 attribute, 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.
A repository method parameter of type Order allows a variable
number of Sort criteria. For example,
Employee[] findByYearHired(int yearHired, Limit maxResults, Order<Employee> sortBy);
...
highestPaidNewHires = employees.findByYearHired(Year.now().getValue(),
Limit.of(10),
Order.by(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,
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 DataException
if the database is incapable of ordering the query results using the given
sort criteria.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Sort<T> Create aSortinstance withascending directionthat does not request case insensitive ordering.static <T> Sort<T> ascIgnoreCase(String attribute) Create aSortinstance withascending directionand case insensitive ordering.static <T> Sort<T> Create aSortinstance withdescending directionthat does not request case insensitive ordering.static <T> Sort<T> descIgnoreCase(String attribute) Create aSortinstance withdescending directionand case insensitive ordering.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanIndicates whether or not to request case insensitive ordering from a database with case sensitive collation.booleanIndicates whether to sort the entity attribute in ascending order (true) or descending order (false).booleanIndicates whether to sort the entity attribute in descending order (true) or ascending order (false).static <T> Sort<T> Create aSortinstanceproperty()Name of the entity attribute to order by.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Sort
Defines sort criteria for an entity attribute. For more descriptive code, use:
Sort.asc(attributeName)for ascending sort on an entity attribute.Sort.ascIgnoreCase(attributeName)for case insensitive ascending sort on an entity attribute.Sort.desc(attributeName)for descending sort on an entity attribute.Sort.descIgnoreCase(attributeName)for case insensitive descending sort on an entity attribute.
- Parameters:
property- name of the entity attribute to order by.isAscending- whether ordering for this attribute 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 entity attribute to order by.- Returns:
- The attribute 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
ignoreCasevalue.- Returns:
- Returns whether or not to request case insensitive sorting for the entity attribute.
-
isAscending
public boolean isAscending()Indicates whether to sort the entity attribute in ascending order (true) or descending order (false).- Returns:
- Returns whether sorting for this attribute shall be ascending.
-
isDescending
public boolean isDescending()Indicates whether to sort the entity attribute in descending order (true) or ascending order (false).- Returns:
- Returns whether sorting for this attribute shall be descending.
-
of
Create aSortinstance- Type Parameters:
T- entity class of the sortable entity attribute.- Parameters:
attribute- name of the entity attribute to order bydirection- the direction in which to order.ignoreCase- whether to request a case insensitive ordering.- Returns:
- a
Sortinstance. Nevernull. - Throws:
NullPointerException- when there is a null parameter
-
asc
Create aSortinstance withascending directionthat does not request case insensitive ordering.- Type Parameters:
T- entity class of the sortable entity attribute.- Parameters:
attribute- name of the entity attribute to order by- Returns:
- a
Sortinstance. Nevernull. - Throws:
NullPointerException- when the attribute name is null
-
ascIgnoreCase
Create aSortinstance withascending directionand case insensitive ordering.- Type Parameters:
T- entity class of the sortable entity attribute.- Parameters:
attribute- name of the entity attribute to order by.- Returns:
- a
Sortinstance. Nevernull. - Throws:
NullPointerException- when the attribute name is null.
-
desc
Create aSortinstance withdescending directionthat does not request case insensitive ordering.- Type Parameters:
T- entity class of the sortable entity attribute.- Parameters:
attribute- name of the entity attribute to order by- Returns:
- a
Sortinstance. Nevernull. - Throws:
NullPointerException- when the attribute name is null
-
descIgnoreCase
Create aSortinstance withdescending directionand case insensitive ordering.- Type Parameters:
T- entity class of the sortable entity attribute.- Parameters:
attribute- name of the entity attribute to order by.- Returns:
- a
Sortinstance. Nevernull. - Throws:
NullPointerException- when the attribute name is null.
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
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 '=='.
-