Blog

How to load data objects from the server with sorting

by on February 24, 2015

In one of our other Recipe articles, we wrote about how to load data from the Backendless mbaas server using paging. In fact, data paging is only one of the features available in the data loading API. In this post, we are going to show how to fetch data objects with sorting. The sorting option can be combined with any other data loading feature. For example, you could have both sorting and the “where clause” in your data loading request. For this example, we are going to use the Restaurant-to-go app schema.


Here’s what my data in the Restaurant table looks like:

Consider the Restaurant class below. When our 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.

    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 )
        {
        }
    } );
    
    @objcMembers class Restaurant: NSObject { var cuisine: String? var name: String? var created: Date? }
    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

    Enjoy!