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. Android 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.setSortBy( "name", "age DESC" );
Backendless.Data.of( "Person" ).find( queryBuilder, 
                                      new AsyncCallback<List<Map>>()
  {
    @Override
    public void handleResponse( List<Map> response )
    {
      // the "response" object is a collection of java.util.Map objects.
      // each item in the collection represents an object from the "Person" table
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
       // use the getCode(), getMessage() or getDetail() on the fault object
       // to see the details of the error
    }
  });
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setSortBy( "name", "age DESC" );
Backendless.Data.of( Person.class ).find( queryBuilder, 
                                          new AsyncCallback<List<Person>>()
  {
    @Override
    public void handleResponse( List<Person> response )
    {
      // the "response" object is a collection of Person objects.
      // each item in the collection represents an object from the "Person" table 
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
       // use the getCode(), getMessage() or getDetail() on the fault object
       // to see the details of the error
    }
    });

Important

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