Package jakarta.data

Record Class 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(String property, boolean isAscending, boolean ignoreCase) extends Record

Sort allows the application to dynamically provide sort criteria which includes a case sensitivity request, a Direction and a property.

Dynamic Sort criteria can be specified when requesting a page of results or can be optionally specified as parameters to a repository method in any of the positions that are after the query parameters. You can use Sort... to allow a variable number of 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"));
 

When combined on a method with static sort criteria (OrderBy keyword or OrderBy annotation or Query with an ORDER BY clause), the static sort criteria is applied first, followed by the dynamic sort criteria that is defined by Sort instances in the order listed.

A repository method will fail with a DataException or a more specific subclass if

  • a Sort parameter is specified in combination with a Pageable parameter with Pageable.sorts().
  • the database is incapable of ordering with the requested sort criteria.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Sort(String property, boolean isAscending, boolean ignoreCase)
    Defines sort criteria for an entity property.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Sort
    asc(String property)
    Create a Sort instance with ascending direction Direction.ASC that does not request case insensitive ordering.
    static Sort
    Create a Sort instance with ascending direction Direction.ASC and case insensitive ordering.
    static Sort
    desc(String property)
    Create a Sort instance with descending direction Direction.DESC that does not request case insensitive ordering.
    static Sort
    Create a Sort instance with descending direction Direction.DESC and case insensitive ordering.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    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 Sort
    of(String property, Direction direction, boolean ignoreCase)
    Create a Sort instance
    Name of the property to order by.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Sort

      public Sort(String property, boolean isAscending, boolean ignoreCase)

      Defines sort criteria for an entity property. For more descriptive code, use:

      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

      public String 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

      public static Sort of(String property, Direction direction, boolean ignoreCase)
      Create a Sort instance
      Parameters:
      property - the property name to order by
      direction - the direction in which to order.
      ignoreCase - whether to request a case insensitive ordering.
      Returns:
      an Sort instance. Never null.
      Throws:
      NullPointerException - when there is a null parameter
    • asc

      public static Sort asc(String property)
      Create a Sort instance with ascending direction Direction.ASC that does not request case insensitive ordering.
      Parameters:
      property - the property name to order by
      Returns:
      a Sort instance. Never null.
      Throws:
      NullPointerException - when the property is null
    • ascIgnoreCase

      public static Sort ascIgnoreCase(String property)
      Create a Sort instance with ascending direction Direction.ASC and case insensitive ordering.
      Parameters:
      property - the property name to order by.
      Returns:
      a Sort instance. Never null.
      Throws:
      NullPointerException - when the property is null.
    • desc

      public static Sort desc(String property)
      Create a Sort instance with descending direction Direction.DESC that does not request case insensitive ordering.
      Parameters:
      property - the property name to order by
      Returns:
      a Sort instance. Never null.
      Throws:
      NullPointerException - when the property is null
    • descIgnoreCase

      public static Sort descIgnoreCase(String property)
      Create a Sort instance with descending direction Direction.DESC and case insensitive ordering.
      Parameters:
      property - the property name to order by.
      Returns:
      a Sort instance. Never null.
      Throws:
      NullPointerException - when the property is null.
    • toString

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.