sort()

Mission

The .sort() modifier specifies the ordering direction for query results.

The Payoff

By completing this module, you will understand how to order query results alphabetically, numerically, or chronologically.

Estimated Time: 2 minutesModules: 2 Concepts

Select sorting attribute keys

Establish the document property path (e.g. 'price' or 'createdAt') to sort by.

Define ordering directions

Provide direction arguments: 'asc' (alphabetical/chronological/lowest-first) or 'desc' (highest-first).
Interactive Code Snippet
typescript
// Sort documents by price descending (highest first)const products = await sdk.collection("products")  .sort("price", "desc")  .get();
Field Validation
The field name argument cannot be empty. If you pass an empty string, the SDK throws a validation exception immediately.
Sort Overwrite Behavior
Calling `.sort()` multiple times on the same builder instance will overwrite any previously configured sorting instructions.

Verify non-empty keys

Ensure the field name argument is specified. Empty strings trigger compiler or runtime validation exceptions.

Manage duplicate sorting instructions

Remember that subsequent .sort() commands overwrite previous settings on the builder instance.