org.h2.result

## Class SortOrder

- java.lang.Object
- - org.h2.result.SortOrder

All Implemented Interfaces:
  java.util.Comparator<Value[]>

    public final class SortOrder
    extends java.lang.Object
    implements java.util.Comparator<Value[]>

A sort order represents an ORDER BY clause in a query.

### Field Summary

Fields
`static int` `ASCENDING` This bit mask means the values should be sorted in ascending order.
`static int` `DESCENDING` This bit mask means the values should be sorted in descending order.
`static int` `NULLS_FIRST` This bit mask means NULLs should be sorted before other data, no matter if ascending or descending order is used.
`static int` `NULLS_LAST` This bit mask means NULLs should be sorted after other data, no matter if ascending or descending order is used.

### Constructor Summary

Constructors
Constructor and Description
`SortOrder(SessionLocal session, int[] queryColumnIndexes)` Construct a new sort order object with default sort directions.
`SortOrder(SessionLocal session, int[] queryColumnIndexes, int[] sortType, java.util.ArrayList<QueryOrderBy> orderList)` Construct a new sort order object.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`static int[]` `addNullOrdering(Database database, int[] sortTypes)` Add explicit `NULLS_FIRST` or `NULLS_LAST` where they aren't already specified.
`int` `compare(Value[] a, Value[] b)` Compare two expression lists.
`Column` `getColumn(int index, TableFilter filter)` Get the column for the given table filter, if the sort column is for this filter.
`java.util.ArrayList<QueryOrderBy>` `getOrderList()` Returns the original query order list.
`int[]` `getQueryColumnIndexes()` Get the column index list.
`java.util.Comparator<Value>` `getRowValueComparator()` Returns comparator for row values.
`int[]` `getSortTypes()` Get the sort order bit masks.
`int[]` `getSortTypesWithNullOrdering()` Returns sort order bit masks with `NULLS_FIRST` or `NULLS_LAST` explicitly set.
`java.lang.StringBuilder` `getSQL(java.lang.StringBuilder builder, Expression[] list, int visible, int sqlFlags)` Create the SQL snippet that describes this sort order.
`void` `sort(java.util.ArrayList<Value[]> rows)` Sort a list of rows.
`void` `sort(java.util.ArrayList<Value[]> rows, int fromInclusive, int toExclusive)` Sort a list of rows using offset and limit.
`static void` `typeToString(java.lang.StringBuilder builder, int type)` Appends type information (DESC, NULLS FIRST, NULLS LAST) to the specified statement builder.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface java.util.Comparator

`comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

### Field Detail

#### ASCENDING

    public static final int ASCENDING

This bit mask means the values should be sorted in ascending order.

See Also:
  Constant Field Values

#### DESCENDING

    public static final int DESCENDING

This bit mask means the values should be sorted in descending order.

See Also:
  Constant Field Values

#### NULLS_FIRST

    public static final int NULLS_FIRST

This bit mask means NULLs should be sorted before other data, no matter if ascending or descending order is used.

See Also:
  Constant Field Values

#### NULLS_LAST

    public static final int NULLS_LAST

This bit mask means NULLs should be sorted after other data, no matter if ascending or descending order is used.

See Also:
  Constant Field Values

### Constructor Detail

#### SortOrder

    public SortOrder(SessionLocal session,
                     int[] queryColumnIndexes)

Construct a new sort order object with default sort directions.

Parameters:
  `session` - the session
  `queryColumnIndexes` - the column index list

#### SortOrder

    public SortOrder(SessionLocal session,
                     int[] queryColumnIndexes,
                     int[] sortType,
                     java.util.ArrayList<QueryOrderBy> orderList)

Construct a new sort order object.

Parameters:
  `session` - the session
  `queryColumnIndexes` - the column index list
  `sortType` - the sort order bit masks
  `orderList` - the original query order list (if this is a query)

### Method Detail

#### getSQL

    public java.lang.StringBuilder getSQL(java.lang.StringBuilder builder,
                                          Expression[] list,
                                          int visible,
                                          int sqlFlags)

Create the SQL snippet that describes this sort order. This is the SQL snippet that usually appears after the ORDER BY clause.

Parameters:
  `builder` - string builder to append to
  `list` - the expression list
  `visible` - the number of columns in the select list
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

#### typeToString

    public static void typeToString(java.lang.StringBuilder builder,
                                    int type)

Appends type information (DESC, NULLS FIRST, NULLS LAST) to the specified statement builder.

Parameters:
  `builder` - string builder
  `type` - sort type

#### compare

    public int compare(Value[] a,
                       Value[] b)

Compare two expression lists.

Specified by:
  `compare` in interface `java.util.Comparator<Value[]>`
Parameters:
  `a` - the first expression list
  `b` - the second expression list
Returns:
  the result of the comparison

#### sort

    public void sort(java.util.ArrayList<Value[]> rows)

Sort a list of rows.

Parameters:
  `rows` - the list of rows

#### sort

    public void sort(java.util.ArrayList<Value[]> rows,
                     int fromInclusive,
                     int toExclusive)

Sort a list of rows using offset and limit.

Parameters:
  `rows` - the list of rows
  `fromInclusive` - the start index, inclusive
  `toExclusive` - the end index, exclusive

#### getQueryColumnIndexes

    public int[] getQueryColumnIndexes()

Get the column index list. This is the column indexes of the order by expressions within the query.

For the query "select name, id from test order by id, name" this is {1, 0} as the first order by expression (the column "id") is the second column of the query, and the second order by expression ("name") is the first column of the query.

Returns:
  the list

#### getColumn

    public Column getColumn(int index,
                            TableFilter filter)

Get the column for the given table filter, if the sort column is for this filter.

Parameters:
  `index` - the column index (0, 1,..)
  `filter` - the table filter
Returns:
  the column, or null

#### getSortTypes

    public int[] getSortTypes()

Get the sort order bit masks.

Returns:
  the list

#### getOrderList

    public java.util.ArrayList<QueryOrderBy> getOrderList()

Returns the original query order list.

Returns:
  the original query order list

#### getSortTypesWithNullOrdering

    public int[] getSortTypesWithNullOrdering()

Returns sort order bit masks with `NULLS_FIRST` or `NULLS_LAST` explicitly set.

Returns:
  bit masks with either `NULLS_FIRST` or `NULLS_LAST` explicitly set.

#### addNullOrdering

    public static int[] addNullOrdering(Database database,
                                        int[] sortTypes)

Add explicit `NULLS_FIRST` or `NULLS_LAST` where they aren't already specified.

Parameters:
  `database` - the database
  `sortTypes` - bit masks
Returns:
  the specified array with possibly modified bit masks

#### getRowValueComparator

    public java.util.Comparator<Value> getRowValueComparator()

Returns comparator for row values.

Returns:
  comparator for row values.