Backendless Support
 

Loading data objects from server with sorting

Backendless Service Platform offers a beneficial feature of fetching data objects with sorting. The sorting option can be combined with any other data loading feature. 

For example, a developer could have both sorting and the "where clause" in your data loading request. For this exact example, the Restaurant-to-go app schema is used.

Below the sample data in the Restaurant table are provided: 

Consider the Restaurant class below. When the code fetches data from the server, it will arrive as a collection of the Restaurant class instances:

package com.mbaas.sample;
import java.util.Date;
public class Restaurant
{
 public String cuisine;
 public String name;
 public Date created;
 public Date updated;
}

The class declares public fields, however, it could've been written as a Java bean (that is with public getters and setters). The Backendless SDK for Java/Android can work either one of the approaches.

The code below loads the restaurant objects sorted in the ascending order by the "created" column/field. The ascending order is requested by the "ASC" qualifier.

Asynchronous sample (Plain Java and Android):
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
QueryOptions queryOptions = new QueryOptions();
queryOptions.addSortByOption( "created ASC" );
dataQuery.setQueryOptions( queryOptions );
// fetch restaurants
 Backendless.Data.of( Restaurant.class ).find( dataQuery, new AsyncCallback<BackendlessCollection<Restaurant>>()
{
 @Override
 public void handleResponse( BackendlessCollection<Restaurant> restaurants )
 {
 // get first page
 List<Restaurant> firstPage = restaurants.getCurrentPage();
 // iterate over the received objects
 Iterator<Restaurant> iterator = firstPage.iterator();
 while( iterator.hasNext() )
 {
 Restaurant restaurant = iterator.next();
 System.out.println( "Restaurant - " + restaurant.name + ", cuisine - " + restaurant.cuisine + ", created on " + restaurant.created );
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 }
} );
Synchronous sample (Plain Java only):
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
QueryOptions queryOptions = new QueryOptions();
queryOptions.addSortByOption( "created ASC" );
dataQuery.setQueryOptions( queryOptions );
// fetch restaurants
BackendlessCollection<Restaurant> restaurants = Backendless.Data.of( Restaurant.class ).find( dataQuery );
// get first page
List<Restaurant> firstPage = restaurants.getCurrentPage();
// iterate over the received objects
Iterator<Restaurant> iterator = firstPage.iterator();
while( iterator.hasNext() )
{
 Restaurant restaurant = iterator.next();
 System.out.println( "Restaurant - " + restaurant.name + ", cuisine - " + restaurant.cuisine + ", created on " + restaurant.created );
}
The code produces the following output - the objects are sorted by the "created" column:
Restaurant - Endless Sweets, cuisine - bakery, created on Wed Jan 14 18:00:00 CST 2015
Restaurant - McDonald's, cuisine - fastfood, created on Fri Jan 16 18:00:00 CST 2015
Restaurant - Buca Di Bepo, cuisine - italian, created on Fri Jan 16 18:00:00 CST 2015
Restaurant - Cantina Laredo, cuisine - mexican, created on Fri Jan 16 18:00:00 CST 2015
Restaurant - Mister Cat, cuisine - pizzeria, created on Wed Feb 18 18:00:00 CST 2015
Restaurant - Evraziya, cuisine - japanese, created on Wed Feb 18 18:00:00 CST 2015

Review related topic:

Is article helpful?