The SPACEtag® Objects API allows you to specify what order you want your data to be returned from the Object Store.
Introduction to Sorting #
Let’s say you have a list of people and you want them returned in order of last name then first name (if they have the same last name). We can do this using the _$sortBy command. Commands in the Objects API always start with _$ so that they can be distinguished from field names.
For example, to return all of your Objects in the contacts.person Collection in Last Name alphabetical order, we could use the following request:
GET https://api.spacetagcloud.net/objects/v1/action?object=contacts.person&_$sortBy=last_name
If we wanted to return the Objects in reverse alphabetical order (e.g. Last Name from Z to A) we can do that as well, using the following request:
GET https://api.spacetagcloud.net/objects/v1/action?object=contacts.person&_$sortBy=last_name.desc
In this example, we have appended the operation .desc to the field name last_name. This tells the data engine to return the data in descending order of Last Name.
We can also add multiple sort fields to our command. The following example will return the Objects in order of Last Name, and then First Name where there are multiple Objects with the same Last Name.
GET https://api.spacetagcloud.net/objects/v1/action?object=contacts.person&_$sortBy=last_name,first_name
This is simply done by separating the sort-by fields with commas (without spaces).
Sorting works on any field DataType including Text, Numbers and DateTimes.
Further Examples #
Example: Date of Birth from latest to oldest, and then by Last Name and First Name where there are multiple people with the same Date of Birth.
GET https://api.spacetagcloud.net/objects/v1/action?object=contacts.person&_$sortBy=dob.desc,last_name,first_name
Example: Combine with a Filter to return people by Last Name and First Name, where they only live in the United Kingdom. This is done by filtering on the country_code field which stores the 3-digit country code for the Person Object.
GET https://api.spacetagcloud.net/objects/v1/action?object=contacts.person&country_code=GBR&_$sortBy=last_name,first_name
