Order By
order_by
query parameter allows clients to customize the sorting of resources in the API response. By specifying fields and sort directions, you can control the order in which results are returned. This feature is especially useful for creating ordered views or paginated responses.Usage
order_by
parameter supports two formats:1.
?order_by=field:asc,another_field:desc
2.
?order_by[]=field:asc&order_by[]=another_field:desc
Sort Direction
asc
for ascending order (default)desc
for descending orderSorting Behavior
Examples
Single Field Sorting
GET /api/resource?order_by=name:asc
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
Multiple Fields Sorting
GET /api/resource?order_by=name:asc,description:desc
name
in ascending order.name
, they are further sorted by description
in descending order.[
{ "id": 1, "name": "Alice", "description": "Zebra" },
{ "id": 2, "name": "Alice", "description": "Apple" },
{ "id": 3, "name": "Bob", "description": "Monkey" }
]
Array-style Query
GET /api/resource?order_by[]=name:asc&order_by[]=description:desc
Error Handling
400 Bad Request
response with an error message detailing the issue.Summary
order_by
to control the sorting of API responses.order_by
parameter, you can efficiently organize API responses to suit your application needs.Modified at 2025-01-26 14:22:48