Sorting
Learn how to sort your api.vision resources to organize results in a meaningful way.
Basic Sorting
To sort results, use the sort query parameter with the name of the field to sort by:
GET /users?sort=lastNameBy default, sorting is in ascending order (A-Z, 0-9). To sort in descending order, add a minus sign (-) before the field name:
GET /products?sort=-priceMulti-Field Sorting
You can sort by multiple fields by separating them with commas. The order matters - results are sorted by the first field, then ties are broken by the second field, and so on:
GET /users?sort=role,lastName,firstNameYou can mix ascending and descending sorts in a multi-field sort:
GET /products?sort=-category,nameThis sorts products by category in descending order (Z-A), and within each category, sorts by name in ascending order (A-Z).
Sorting on Related Resources
You can sort based on fields in related resources using dot notation:
GET /posts?sort=author.lastNameThis sorts posts by the last name of the author. You can go multiple levels deep:
GET /comments?sort=post.author.lastNameSorting on Nested Fields
For resources with nested objects, you can sort by nested fields using dot notation:
GET /products?sort=specs.weightThis sorts products by the weight field within the specs object.
Sorting on Array Fields
When sorting on array fields, api.vision uses the first element of the array for comparison:
GET /products?sort=tagsFor more advanced array sorting needs, consider using a custom field or scripting.
Sorting Null Values
By default, null values are sorted at the beginning for ascending sorts and at the end for descending sorts. You can control this behavior with the nulls parameter:
GET /users?sort=lastLogin&nulls=lastOptions for the nulls parameter:
first- null values appear firstlast- null values appear last
Sorting Types
api.vision automatically determines the appropriate sort method based on the field type:
| Field Type | Sort Behavior |
|---|---|
| String | Alphabetical (lexicographic) comparison |
| Number | Numerical comparison |
| Date/Time | Chronological comparison |
| Boolean | false before true (ascending) |
Combining Sorting with Other Query Features
Sorting can be combined with filtering, pagination, and other query features:
GET /products?category=electronics&price_gte=100&sort=-rating,price&page=1&limit=20This query:
- Filters for electronics products with a price of 100 or more
- Sorts by rating (descending) and then by price (ascending)
- Returns the first page of results with 20 items per page
Advanced Example
GET /orders?
status=shipped&
sort=-orderDate,customer.lastName,customer.firstName&
expand=customer,items&
page=1&
limit=10This query fetches shipped orders, sorted first by order date (newest first), then by customer's last name, and finally by customer's first name. It includes the related customer and items, paginated to 10 items per page.
Performance Tip
Sorting on fields in related resources (using dot notation) can be more resource-intensive than sorting on direct fields. If performance is a concern, consider adding the most common sort fields directly to the main resource.