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. .NET applications must use the DataQueryBuilder class to set the sorting option. The example below configures DataQueryBuilder to sort results by the name and age columns. Value in the age column are sorted in the descending order. The query is used to retrieve objects from the Person table:

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddSortBy( "name" );
queryBuilder.AddSortBy( "age DESC" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result = Backendless.Data.Of( "Contact" ).Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
  foundObjects =>
  {
    System.Console.WriteLine( "Server returned " + foundObjects.Count + " objects" );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Contact" ).Find( queryBuilder, findCallback );
DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddSortBy( "name" );
queryBuilder.AddSortBy( "age DESC" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Contact> result = Backendless.Data.Of<Contact>().Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Contact>> findCallback;
findCallback = new AsyncCallback<IList<Contact>>(
  foundObjects =>
  {
    System.Console.WriteLine( "Server returned " + foundObjects.Count + " objects" );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of<Contact>().Find( queryBuilder, findCallback );

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