Support Topics Documentation Slack YouTube Blog

Trying to get objects and sort them by name by sorting isn't happening

My App has a background service to do some function which is Retreive Objects with sort by name so I can find a specific Element at a specific index, but can’t sort them

Here is my Code

AsyncCallback<Integer> callback3= new AsyncCallback<Integer>() {

        @Override
        public void handleResponse(Integer response) {
            objectscount=response;
            totalPagesCount= objectscount/15;
            if ((objectscount%15) >= 1 && (objectscount%15) <= 15){
                totalPagesCount = totalPagesCount + 1;
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    };
    dataQuery = DataQueryBuilder.create();
    dataQuery.setPageSize(PAGESIZE);
    dataQuery.setSortBy("name");
    Backendless.Data.of(Movie.class).getObjectCount(dataQuery,callback3);

 AsyncCallback<List<Movie>> callback = new AsyncCallback<List<Movie>>() {
                @Override
                public void handleResponse(List<Movie> response) {

                    for (Movie movie : response) {
                        if (movie.getName()!=null) {
                            movies.add(movie);
                        }
                    }
                }

                @Override
                public void handleFault(BackendlessFault fault) {
                }
            };
            Backendless.Data.of(Movie.class).find(dataQuery,callback);

            Handler handler2= new Handler();
            handler2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < totalPagesCount; i++) {

                            dataQuery.prepareNextPage();
                            AsyncCallback<List<Movie>> callback2 = new  AsyncCallback<List<Movie>>() {
                                @Override
                                public void handleResponse(List<Movie> response) {

                                    for (Movie movie : response) {
                                        if (movie.getName()!=null) {
                                            movies.add(movie);
                                        }
                                    }
                                }

                                @Override
                                public void handleFault(BackendlessFault fault) {
                                }
                            };
                            Backendless.Data.of(Movie.class).find(dataQuery,callback2);
                        }

                    }

            },3000);

Hi, Peter.
Here is my simple example and it works

DataQueryBuilder queryBuilder = DataQueryBuilder.create().setSortBy( "name" );
List<Map> result = Backendless.Data.of( "test_table"). find(queryBuilder );

If you iterate over objects in the list you’ll see, that they all were sorted by column name.

Could you, please post here the minimal example showing the problem with your data set. Also do not forget to point your appid.

I have many columns, I want to sort objects in inside the column by objects name, objects start with letter a come first then b ,etc

I cannot help you without your help.
I’ve tried with simple example and it works.

Could you, please post here the minimal example (code snippet, not entire code) demonstrating the problem with your data set (table with the 5-6 objects will be enough).
Also do not forget to point your appid.

appid: 557DE22F-110B-81E5-FF9A-0CB7813A6000

When I Print movie Name at index 50 in movies List by Toast message, each time it retrieve another movie name, which means the objects are not retrieved with sort by name

Code

public class MyService extends JobService {

List<com.notifyuser.peter.notify.Movie> movies=new ArrayList<>();
byte PAGESIZE =15;
int objectscount=0;
DataQueryBuilder dataQuery;
int totalPagesCount;
private static final String TAG="MyService";

@Override
public boolean onStartJob(JobParameters params) {
    Log.d(TAG,"jobstarted");
    backGroundService(params);
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    return true;
}

private void backGroundService(final JobParameters params) {


    AsyncCallback<Integer> callback3= new AsyncCallback<Integer>() {

        @Override
        public void handleResponse(Integer response) {
            objectscount=response;
            totalPagesCount= objectscount/15;
            if ((objectscount%15) >= 1 && (objectscount%15) <= 15){
                totalPagesCount = totalPagesCount + 1;
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    };
    dataQuery = DataQueryBuilder.create();
    dataQuery.setPageSize(PAGESIZE);
    dataQuery.setSortBy("name");
    Backendless.Data.of(Movie.class).getObjectCount(dataQuery,callback3);

    movies.clear();

                AsyncCallback<List<Movie>> callback = new AsyncCallback<List<Movie>>() {
                @Override
                public void handleResponse(List<Movie> response) {

                    for (Movie movie : response) {
                        if (movie.getName()!=null) {
                            movies.add(movie);
                        }
                    }

                }

                @Override
                public void handleFault(BackendlessFault fault) {
                }
            };
            Backendless.Data.of(Movie.class).find(dataQuery,callback);

            Handler handler2= new Handler();
            handler2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < totalPagesCount; i++) {

                            dataQuery.prepareNextPage();
                            AsyncCallback<List<Movie>> callback2 = new AsyncCallback<List<Movie>>() {
                                @Override
                                public void handleResponse(List<Movie> response) {

                                    for (Movie movie : response) {
                                        if (movie.getName()!=null) {
                                            movies.add(movie);
                                        }
                                    }
                                }

                                @Override
                                public void handleFault(BackendlessFault fault) {
                                }
                            };
                            Backendless.Data.of(Movie.class).find(dataQuery,callback2);
                        }

                    }

            },3000);

          Toast.makeText(MyService.this, ""+movies.get(50).getName, Toast.LENGTH_SHORT).show();


            jobFinished(params,true);
        }
  }

You posted the same code with your custom logic, where you do count, find, add operations, pagination.
I was talking about the simple code, where you f.e. retrieve the set of objects and the same object would be on different places.
Please, try with the simpler code.

DataQueryBuilder queryBuilder = DataQueryBuilder.create().setSortBy( "name" ).setPageSize( 100 );
List<Map> result = Backendless.Data.of( "Movie"). find(queryBuilder );
System.out.println(result.get( 53 ).get( "name" ));

If you invoke above code several times you’ll see, the object is on the same place (of course if you didn’t modify other objects in the table).

What I meant is, when I get the data, they aren’t retrieved as displayed here in the website, setSortBy(“name”) does sort the object by there names for every page but it doesn’t retrieved all the objects and sort them

Hello @peter_estafanos

So, is your problem with paging or with sorting?
You can not load more than 100 in one request, however, using paging API you can retrieve as many objects as you need.

Regards, Vlad