Sorting

Using the JavaScript SDK is the easiest way to sort a collection <Link to JS SDK sort section>. However, sorting is done by adding the sort query string parameter to a collection request when choosing to sort directly. If no sort is provided, Datastore defaults to sorting in the order in which documents were created.

To change the direction of the sort + or - is added before the property name in the sort query string. Sorting defaults to ascending; however, you can still add + before the property. To sort descending, add - before the property name.

Sorting is done on document or source properties specified after the sort query string, for example, to sort by document property username ascending using CURL;

curl \
    -X GET \
    -H "Content-Type: application/json" \
    --silent \
    -fS https://my-data-service.datastore.squiz.cloud/my-first-collection?sort=document.username

To do the same request sorting username descending using CURL;

curl \
    -X GET \
    -H "Content-Type: application/json" \
    --silent \
    -fS https://my-data-service.datastore.squiz.cloud/my-first-collection?sort=-document.username

The ability to sort by multiple properties at once is achieved by separating each property by a comma in the sort query string. For example, to sort by the document property username ascending and the source property $createdDatetime descending using CURL;

curl \
    -X GET \
    -H "Content-Type: application/json" \
    --silent \
    -fS https://my-data-service.datastore.squiz.cloud/my-first-collection?sort=+document.username,-document.$createdDatetime

Datastore always ensures documents are returned in a consistent order. Datastore sorts documents by the order they were created for any situation wherein ordering may be ambiguous (for example, sorting by a property and two documents store the same value).