Ordering
Ordering the results
There are a couple of query options that gives you control over how your results are ordered.
Two keywords exist for defining sorting in a query text:
- .SORT
- .REVERSESORT
You can use both to name one or more fields. Results will be returned in the given order (ascending or descending, depending on the given keyword).
Order by a field - lowest to highest
Following query returns the cars ordered by their name:
Copy
GET https://example.com/OData.svc/Root?query=Type:Car .SORT:Name
🛈 Special characters should be URL encoded
Order by a field - highest to lowest
The next example query returns the same content as the above one but in reverse order:
Copy
GET https://example.com/OData.svc/Root?query=Type:Car .REVERSESORT:Name
🛈 Special characters should be URL encoded
Order by multiple fields
Following query example shows how to order results by multiple fields, in this case cars by their color and name:
Copy
GET https://example.com/OData.svc/Root?query=Type:Car .SORT:Color .SORT:Name
🛈 Special characters should be URL encoded
Order by multiple fields in different directions
In the following examples we combine all the things above with sorting cars first by then by their color in reverse order and the name in ascending order.
Copy
GET https://example.com/OData.svc/Root?query=Type:Car .REVERSESORT:Color .SORT:Name
🛈 Special characters should be URL encoded
Order by date
Ordering cars by their StartingDate
field:
Copy
GET https://example.com/OData.svc/Root?query=Type:Car .SORT:StartingDate
🛈 Special characters should be URL encoded