orderBy

abstract fun orderBy(column: KQLiteColumn<*>): SelectStatement<T>
abstract fun orderBy(column: OrderColumn): SelectStatement<T>

Same as calling orderBy(column, null)


abstract fun orderBy(column: KQLiteColumn<*>, sort: Sort?): SelectStatement<T>

Adds an ORDER BY clause to the SELECT statement to sort the result set.

This function sorts the rows returned by the query based on the values in a specified column. By default, the sorting order is ascending (ASC). You can specify descending order (DESC) using the sort parameter.

This overload is for sorting by a single column. To sort by multiple columns with different sort orders, use the overload that accepts OrderColumn instances.

Example

    // Selects all employees and orders them by their last name.
val ascendingCursor =
Employees
.select()
.orderBy(Employees.lastName) // Sort not applied
.execute()

// Selects all employees and orders them by their age in descending order.
val descendingCursor =
Employees
.select()
.orderBy(Employees.age, Sort.DESC)
.execute()

Return

The SelectStatement instance, allowing for further chaining of query-building methods.

Author

MOHAMMAD AZIM ANSARI

Parameters

column

The KQLiteColumn to sort the result set by.

sort

The sorting order (Sort.ASC or Sort.DESC). If null, defaults to ascending.

See also


abstract fun orderBy(orderColumn1: OrderColumn, orderColumn2: OrderColumn?): SelectStatement<T>

Adds an ORDER BY clause to the SELECT statement to sort the result set by one or more columns, each with its own specific sorting order.

This overload is designed for complex sorting scenarios where you need to specify different sort directions (ascending/descending) for multiple columns. You achieve this by wrapping each column and its desired sort order in an OrderColumn object.

Example

// Selects employees, ordering them first by department ID in ascending order,
// and then by salary in descending order within each department.
val cursor =
Employees
.select()
.orderBy(
orderColumn1 = OrderColumn(Employees.departmentId, null, Sort.ASC),
orderColumn2 = OrderColumn(Employees.salary, null, Sort.DESC)
)
.execute()

Return

The SelectStatement instance, allowing for further chaining of query-building methods.

Author

MOHAMMAD AZIM ANSARI

Parameters

orderColumn1

The primary OrderColumn defining the first level of sorting.

orderColumn2

An optional secondary OrderColumn for the second level of sorting.

See also