Skip to content

Data retrieval with Sorting

Data retrieval and search API can request the server to return sorted data. Data can be sorted by one or more columns. The sorting direction can be either ascending (default) or descending for each individual column. To request sorting with the REST API use the sortBy parameter in a data retrieval request:

https://xxxx.backendless.app/api/data/<table-name>?sortBy=prop1,prop2

sortBy can be used with all other parameters supported by Backendless. The following sample request retrieves objects from the Person table sorted by the name (descending order) and age (ascending order) columns:

curl "https://xxxx.backendless.app[`/api/data/Person?sortBy=name%20desc%2Cage%20asc`](/api/data/Person?sortBy=name%20desc%2Cage%20asc)"

Important

The descending sorting order can be requested by adding DESC to the name of the column (separated by space).

Codeless Reference

Consider the following data table called Movie:

data_service_count_example

Suppose you need to retrieve records from the data table, but you want to have them sorted in the response by values in a specific column. To do that, you need to reference the name of the column in the sort by property.

The example below retrieves records from the data table and sorts them by values stored in the totalBoxOffice column.

data_service_sorting_example_1

Important

For a detailed description of all input parameters see the Basic Object Retrieval topic of this guide.

The result of this operation is eight objects sorted by values in the totalBoxOffice property:

data_service_sorting_example_2

You can also perform double sorting of records by referencing the second column name in the sort by property. The double sorting works only if there are two or more similar values stored in the first column. Once the operation runs, it sorts all values by the first column name and then it sorts values by the second column name.

For demonstration purposes, the data table presented below contains two similar records:

data_service_sorting_example_3

The example below retrieves records from the data table and sorts them by values stored in two columns: totalBoxOffice and yearReleased. It uses an additional argument in the sort by property called "desc" that instructs the operation to sort values in descending order. The ascending order is represented as the "asc" argument.

data_service_sorting_example_4

After the Codeless Logic runs, the operation returns objects from the data table sorted by both columns referenced in the sort by property. As you can see, two objects with the smallest  values in the "totalBoxOffice" column returned in the response were sorted by both columns, while all other objects were practically sorted only by the first column. It was sorted in this way because the operation considers the first column name referenced in the sort by property as the primary, which forces the operation to find similar or identical values in the specified column first, and then if it sorts similar or identical values in the second column.

data_service_sorting_example_5