Sorting results

How to request a specific sort for the results of a collection of resources

When you fetch a collection of resources, you can request a specific sort for the results, using the sort query parameter.

The value of the sort parameter must be a comma-separated list of fields. Pay attention to avoid whitespaces before or after each comma.

The sort order for each field is ascending unless prefixed with a - (minus) in which case it's descending.

Sortable fields

Not all resource attributes can be used as sorting parameters. You can get the full list of the sortable attributes from the documentation of each resource in the "List all" section (e.g. sortable fields for SKUs).

Please note that if you try to sort a collection of resources by a non-sortable attribute the API which will respond with a 400 Bad Request status code, due to invalid sort criteria.

Examples

Sorting the parent resource

The following request fetches a collection of SKUs sorted alphabetically by their code:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus?sort=code' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

The following request fetches the list of orders associated with a certain customer, sorted by their creation date (descending):

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/customers/xYZkjABcde/orders?sort=-created_at' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

You can get the full list of sortable attributes from the documentation of each resource.

Last updated