Skip to main content

Order By

Definition

The orderBy method allows you to add the ORDER BY clause to the query.

Available methods

1. orderBy(KColumnAllowedToOrderBy... kColumnsAllowedToOrderBy)

  • kColumnsAllowedToOrderBy: are all the expresions that will be added to the ORDER BY clause.
    Among the possible values are: KTableColumn, KColumn, KRaw.
info

KTableColumn and KColumn objects have the asc and desc methods available to add the ASC or DESC option respectively.

In addition, the nullsFirst and nullsLast methods are also available after calling the asc and desc methods to add the NULLS FIRST or NULLS LAST option respectively.

None of these 4 methods receive parameters.

Method hierarchy

The orderBy method can be used right after the following methods:

and the subsequent methods that can be called are:

Example: KTableColumn

Java code:

k
.select(
APP_USER.FIRST_NAME,
APP_USER.LAST_NAME
)
.from(APP_USER)
.orderBy(APP_USER.ID.desc().nullsFirst())
.multiple();

SQL generated:

SELECT au.first_name, au.last_name
FROM app_user au
ORDER BY au.id DESC NULLS FIRST

Parameters:

  • None

Example: KColumn

Java code:

k
.select(
APP_USER.FIRST_NAME,
APP_USER.LAST_NAME
)
.from(APP_USER)
.orderBy(concat(APP_USER.FIRST_NAME, val(" "), APP_USER.LAST_NAME).asc().nullsLast())
.multiple();

SQL generated:

SELECT au.first_name, au.last_name
FROM app_user au
ORDER BY CONCAT(au.first_name || ?1 || au.last_name) ASC NULLS LAST

Parameters:

  • ?1: " "

Example: KRaw

Java code:

k
.select(
APP_USER.FIRST_NAME,
APP_USER.LAST_NAME
)
.from(APP_USER)
.orderBy(raw("au.id DESC"))
.multiple();

SQL generated:

SELECT au.first_name, au.last_name
FROM app_user au
ORDER BY au.id DESC

Parameters:

  • None