order_by
query parameter.
The Miru API uses field-based sorting. You can specify which field(s) to sort by and the sort direction (ascending or descending).
Resource IDs are used as a tie-breaker when multiple resources have equivalent fields to ensure deterministic sort results.
Query Parameters
List endpoints that support sorting accept theorder_by
query parameter, which is optional.
Order By
You can sort the list endpoints’ results using the following format:<field_name>:<direction>
. The direction is optional and defaults to descending if not specified.
To illustrate, the following order by queries are all valid:
order_by=created_at
order_by=created_at:asc
order_by=created_at:desc
order_by
query parameter in the Query Parameters section of the list endpoint you want to sort.
Common sort fields include:
id
- Sort by object IDcreated_at
- Sort by creation timestamp
asc
- Ascending order (A-Z, a-z, 0-9, oldest first, etc.)desc
- Descending order (Z-A, z-a, 9-0, newest first, etc.)
Single Field Sorting
For endpoints that support single field sorting, add theorder_by
query parameter to the list endpoint URL.
The following request sorts config instances by descending creation date using the query parameter order_by=created_at:desc
.
order_by=created_at:asc
.
Multiple Field Sorting
In cases where your primary field sort yields multiple items that hold the same value, you may find it beneficial to sort by auxiliary fields. In such cases, you can specify additional sort fields via a comma-separated list:<field_name_1>:<direction_1>,<field_name_2>:<direction_2>,...
.
Let’s take the example of order_by=created_at:desc,id:asc
:
- First, results are sorted by the
created_at
field in descending order (newest items appear first). - For any items that have the same
created_at
timestamp, the API usesid
as a tie-breaker. - Last, the tied items are sorted by
id
in ascending order (lowest ID first).
created_at
) in descending order. Since elements with IDs 2 and 3 have the same creation date, they are sorted by ID in ascending order.
The following request sorts devices by descending creation date and then by ascending ID:
Deterministic Sorting
By default, all sorting queries use resource IDs as a tie-breaker (ascending order) to ensure deterministic results. For example,created_at:desc
is effecively created_at:desc,id:asc
.
If a request already contains id
as a sort field, then the request is already deterministic and an additional id
sort field will not be added.
To illustrate this, the following table shows the original request ordering and the effective request ordering for a few different examples.
Original | Effective |
---|---|
created_at:desc | created_at:desc,id:asc |
name:asc | name:asc,id:asc |
created_at:desc,id:asc | created_at:desc,id:asc |
id:asc,created_at:desc | id:asc,created_at:desc |