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).