Order by
orderBy
Method Documentation
The orderBy
method specifies the sorting order for query results. It supports single columns, multiple columns, and key-value pairs with optional sorting directions (asc
or desc
).
Note:
After finishing building your query attach .build()
to generate it.
Method Signature
orderBy(
column: keyof T | { [K in keyof T]?: "asc" | "desc" } | string[]
): this;
Parameters
column
:- A string representing a column name. Optionally, append
:asc
or:desc
for sorting direction. - An object where keys are column names and values are sorting directions (
asc
ordesc
). - An array of strings, where each string follows the
column:direction
format.
- A string representing a column name. Optionally, append
Returns
The current instance, enabling method chaining.
Examples
1. Single Column (Default Ascending)
query.orderBy("name");
2. Single Column (Descending)
query.orderBy("email:desc");
3. Multiple Columns (Array)
query.orderBy("name", "desc");
Summary Table
Format | Example | Use Case |
---|---|---|
Single Column | query.orderBy("name") | Ascending by name |
Single Column (Desc) | query.orderBy("email:desc") | Descending by email |