Ordering and Pagination

Ordering and pagination for results

There are a couple of query options that gives you control over your results and how they are ordered. With them you can not only able to create collection results that are sorted in the required order but to implement paging functionality using the $top and $skip options.

Ordering

With the $orderby property you are able to sort collection results by one or more properties and forward or reverse direction. You can specify as many fields as you want.

You can use the orderings option to order your results by a certain field. By default it will sort the field from the lowest value to the highest.

The following examples shows how to sort the results by the DisplayName field:

Copy
GET https://example.com/OData.svc/Root/Content/Cars?$orderby=DisplayName
🛈 Special characters should be URL encoded

Order by a field in an explicit direction

The following examples shows how to sort the results by the Price field from lowest to highest:

Copy
GET https://example.com/OData.svc/Root/Content/Cars?$orderby=Price asc
🛈 Special characters should be URL encoded

Order by a field in reverse order

The following example shows how to sort the results by the StartingDate field from closest to earliest:

Copy
	GET https://example.com/OData.svc/Root/Content/Cars?$orderby=StartingDate desc

Order by a multiple fields

You can specify multiple fields to sort the order of your results by. The results will be ordered first by the first field specified. If any of those values are equal, then those results will be sorted by the next specified field. And so on.

Here is an example that will order the results first by the last modification date of the docs from closest to earliest. It will then sort any docs that have the same StartingDate by their DisplayName.

Copy
GET https://example.com/OData.svc/Root/Content/Cars?$orderby=StartingDate desc,DisplayName
🛈 Special characters should be URL encoded

Top

With the $top option you can limit collection results.

Copy
GET https://example.com/OData.svc/Root/Content/Cars?$top=5
🛈 Special characters should be URL encoded
 

Skip

$skip option is for hiding the first given number of elements from the result.

Copy
GET https://example.com/OData.svc/Root/Content/Cars?$skip=5
🛈 Special characters should be URL encoded
 

Pagination

In case of paging the $top option defines the maximum number of documents that the API will return for your query so that the number of docs that will be listed on a page and $skip defines how many docs will be skipped.

Following example demonstrates how to query the second page of a folder list with 3 folders on a page.

Copy
GET https://example.com/OData.svc/Root/Content/Cars?$top=3&$skip=3
🛈 Special characters should be URL encoded