Skip to main content
The Query Builder’s sort() method allows you to control the order of results in your queries.

Overview

Sort your query results by one or more fields:

Sort Direction

Single Field Sorting

Sort by a single field:

Multiple Field Sorting

Sort by multiple fields by chaining sort() calls:
The sort order is determined by the order you call sort(). The first call has the highest priority.

String Sorting

Sort text fields alphabetically:

Number Sorting

Sort numeric fields:

Date Sorting

Sort by date fields:

Sorting with Filtering

Combine sorting with filtering:

Sorting Relations

Sort on expanded relations:

Common Patterns

Latest Items First

Alphabetical

Priority Sorting

Sort by priority, then by date:

Null Handling

Fields with null values are sorted last:

Complete Example

Performance Considerations

1. Sort on Indexed Fields

For better performance, sort on indexed fields:

2. Limit Results with Pagination

Always paginate sorted queries:

3. Combine Filtering and Sorting

Filter before sorting to reduce the result set:

Reference

sort(field, direction?)

Add sorting to the query. Parameters:
  • field (string) - Field name to sort by
  • direction (string) - Sort direction: "asc" or "desc" (default: "asc")
Returns: QueryBuilder<T> - The query builder for chaining

Next Steps