Skip to main content
Sorting allows you to order API results by one or more fields in ascending or descending order. This is essential for presenting data in a meaningful way to users.

Basic Syntax

The sort parameter uses the following format:
  • field: The name of the field to sort by
  • order: Either ASC (ascending) or DESC (descending)
The field and order are separated by a comma (,), not the standard delimiter (||).

Example

This retrieves users sorted by name in ascending order (A to Z).

Sort Orders

There are two sort orders available:
Sort order is case-sensitive. Use uppercase ASC or DESC. Lowercase values will throw an error.

Ascending Sort

Ascending sort orders results from lowest to highest:

Descending Sort

Descending sort orders results from highest to lowest:

Multiple Sort Fields

You can sort by multiple fields by adding multiple sort parameters:
The order of sort parameters matters:
  • First sort parameter is the primary sort
  • Second sort parameter is applied when primary values are equal
  • And so on…

Example with Multiple Sorts

This sorts users:
  1. First by role (alphabetically)
  2. Within each role, by last name (alphabetically)
  3. Within each last name, by first name (alphabetically)

Sorting Different Data Types

Strings

String sorting is typically case-sensitive and follows lexicographic order.

Numbers

Numbers are sorted numerically (e.g., 1, 2, 10, 100).

Dates

Dates are sorted chronologically.

Booleans

Boolean sorting: false < true in ascending order.

Sorting with Other Parameters

Combine sorting with filtering, pagination, and field selection:

Sort with Filter

Sort with Pagination

Always use the same sort order across paginated requests to ensure consistent results.

Sort with Field Selection

Complete Example

This request:
  1. Filters electronics products that are in stock
  2. Sorts by price (lowest first)
  3. Returns only id, name, and price fields
  4. Limits results to 20 items

Sorting Nested Fields

You can sort by fields in related entities using dot notation:
When sorting by nested fields, make sure to include the appropriate join parameter to load the relation.

Common Sorting Patterns

Latest First

Alphabetical Lists

Priority/Status Based

Error Handling

Invalid sort parameters will throw a RequestQueryException:

Performance Considerations

Database Indexes

For optimal performance, create database indexes on frequently sorted fields:

Composite Indexes

For multi-field sorts, consider composite indexes:

Avoid Sorting Large Datasets

Always combine sorting with pagination to limit the dataset size.

Practical Examples

User Management Dashboard

E-commerce Product Listing

Blog/Content Management

Task Management

Best Practices

  1. Always specify sort order: Include both field and order (ASC/DESC)
  2. Use consistent sorting: Maintain the same sort order across paginated requests
  3. Index sorted fields: Add database indexes to commonly sorted fields
  4. Combine with pagination: Always limit results when sorting
  5. Default to DESC for timestamps: Use sort=createdAt,DESC to show newest items first
  6. Consider multiple sorts: Use multiple sort parameters for ties (e.g., same priority)
  7. Document default sorting: Let API consumers know the default sort behavior

Next Steps

Pagination

Learn how to paginate sorted results

Filtering

Combine sorting with filters

Relations

Sort by fields in related entities

Query Parameters

Overview of all query parameters