When invoking a query on an orderable collection, the client often requires the data
in a sorted order for presentation to the user. Sort criteria are specified by the sortBy
query
parameter.
GET https://www.example.com/reports/reports?sortBy=name,descriptionThis call results in a paginated collection view that is sorted first by the report name, then by the description.
The value of the sortBy
parameter is one or more sort criteria, separated by commas.
Sort criteria consist of a key which is a member of a resource.
Each sort criteria may also have sort options which specify
sort order or collation strength. Note: Not all APIs support collation strength options.
(The values may be specified in the sortBy
parameter but are not honored
by the underlying service.)
sortBy
syntax
The syntax of of the sortBy
sort criteria is
defined by the following Backus–Naur form syntax:
sortCriteria ::= criteria [ ',' sortCriteria ] criteria ::= key [ ':' options ] options ::= option [ ':' options ] option ::= 'ascending' | 'descending' option ::= 'primary' | 'secondary' | 'tertiary' | 'quaternary' | 'identical'
The key
is the name of a member (also known as a field) in the resources that are being sorted. For
example, if a collection resource /someApi/models
contains
a collection of models, and each model has members with names
model
, type
, modifiedTimeStamp
and modifiedBy
, a client can request the collection
of models sorted first by modified date (most recent first), then who
last modified the resource, then by type, then by name:
GET https://www.example.com/someApi/models?sortBy=modifiedTimeStamp:descending,modifiedBy,type,nameIf more than one criteria exist, they are considered subgroup sorting criteria. The order is important: the second sort criteria, if present, applies to items that are equal according to the first sort criteria, and so on. It is unlikely that many resources will have the same
modifiedTimeStamp
.
For the query
GET https://www.example.com/someApi/models?sortBy=modifiedBy,type,modifiedTimeStamp:descending
there are likely to be several models modified by each user, and for each user, several models with the same type. Within those subgroups, the items will be sorted by descending modified timestamp.
sortBy
sort criteria options
Ordering options
are:
ascending
descending
primary
secondary
tertiary
quaternary
identical
tertiary
.primary
or secondary
; for example depending on whether you want á to be grouped with â.If more than one of {ascending,descending}
appear, the last occurrence is used.
If more than one of {primary,secondary,tertiary,identical}
appear, the last occurrence is used.
The default sorting strength is tertiary
.
Consult the documentation for each API to determine the default sort order for its orderable collections.
If it is not documented, you can assume the collection order is not guaranteed and the service MAY return items in a different order in future requests.
For pagination, the first
,
next
, prev
, and last
link relations, if present,
preserve the sortBy
query parameter if it was used. For pagination,
the start
query parameters are relative to sorted collection indexes.
The sort keys are selected from the full set of fields in the collection's underlying resource, which may be a superset of the fields in the requested representation on any specific query.