Class Sort


  • public class Sort
    extends Object

    Utility class to build and represent SQL sorting specifications. A Sort instance represents a list of columns to sort on, each with a direction to use for sorting.

    Usage:

     Sort sort = Sort.by("name").and("age", Direction.Descending);
     Sort sort2 = Sort.ascending("name", "age");
     Sort sort3 = Sort.descending("name", "age");
     
    Author:
    Stéphane Épardaud
    See Also:
    Sort.Direction
    • Method Detail

      • by

        public static Sort by​(String column)
        Sort by the given column, in ascending order.
        Parameters:
        column - the column to sort on, in ascending order.
        Returns:
        a new Sort instance which sorts on the given column in ascending order.
        See Also:
        by(String, Direction), by(String...)
      • by

        public static Sort by​(String column,
                              Sort.Direction direction)
        Sort by the given column, in the given order.
        Parameters:
        column - the column to sort on, in the given order.
        direction - the direction to sort on.
        Returns:
        a new Sort instance which sorts on the given column in the given order.
        See Also:
        by(String), by(String...)
      • by

        public static Sort by​(String column,
                              Sort.NullPrecedence nullPrecedence)
        Sort by the given column, in the given order and in the given null precedence.
        Parameters:
        column - the column to sort on, in the given order.
        nullPrecedence - the null precedence to use.
        Returns:
        a new Sort instance which sorts on the given column in the given order and null precedence.
        See Also:
        by(String), by(String...)
      • by

        public static Sort by​(String column,
                              Sort.Direction direction,
                              Sort.NullPrecedence nullPrecedence)
        Sort by the given column, in the given order and in the given null precedence.
        Parameters:
        column - the column to sort on, in the given order.
        direction - the direction to sort on.
        nullPrecedence - the null precedence to use.
        Returns:
        a new Sort instance which sorts on the given column in the given order and null precedence.
        See Also:
        by(String), by(String...)
      • descending

        public static Sort descending​(String... columns)
        Sort by the given columns, in descending order.
        Parameters:
        columns - the columns to sort on, in descending order.
        Returns:
        a new Sort instance which sorts on the given columns in descending order.
        See Also:
        by(String, Direction), by(String), descending(String...)
      • descending

        public Sort descending()
        Sets the order to descending for all current sort columns.
        Returns:
        this instance, modified.
        See Also:
        ascending(), direction(Direction)
      • direction

        public Sort direction​(Sort.Direction direction)
        Sets the order to all current sort columns.
        Parameters:
        direction - the direction to use for all current sort columns.
        Returns:
        this instance, modified.
        See Also:
        descending(), ascending()
      • and

        public Sort and​(String name)
        Adds a sort column, in ascending order.
        Parameters:
        name - the new column to sort on, in ascending order.
        Returns:
        this instance, modified.
        See Also:
        and(String, Direction)
      • and

        public Sort and​(String name,
                        Sort.Direction direction)
        Adds a sort column, in the given order.
        Parameters:
        name - the new column to sort on, in the given order.
        direction - the direction to sort on.
        Returns:
        this instance, modified.
        See Also:
        and(String)
      • and

        public Sort and​(String name,
                        Sort.NullPrecedence nullPrecedence)
        Adds a sort column, in the given null precedence.
        Parameters:
        name - the new column to sort on, in the given null precedence.
        nullPrecedence - the null precedence to use.
        Returns:
        this instance, modified.
        See Also:
        and(String)
      • and

        public Sort and​(String name,
                        Sort.Direction direction,
                        Sort.NullPrecedence nullPrecedence)
        Adds a sort column, in the given order and null precedence.
        Parameters:
        name - the new column to sort on, in the given order and null precedence.
        direction - the direction to sort on.
        nullPrecedence - the null precedence to use.
        Returns:
        this instance, modified.
        See Also:
        and(String)
      • getColumns

        public List<Sort.Column> getColumns()
        Get the sort columns
        Returns:
        the sort columns
      • empty

        public static Sort empty()
        Creates an Empty Sort instance. Equivalent to #by().
        Returns:
        a new empty Sort instance
        See Also:
        by(String[])