query.orderByRandom with Parse.com? – Displaying records in random order

Tags: , , ,



I understand that you can do the following…

query.orderByAscending("rowValue");
query.orderByDescending("rowValue");

But what if you actually want your data to come out in random order every time your activity is opened? How might this be accomplished?

Answer

There is no built in function for random sort order in the Parse API.

You can randomize the list after you receive it using Collections.shuffle()

Ex.

ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
 query.findInBackground(new FindCallback<ParseObject>() {
     public void done(List<ParseObject> objects, ParseException e) {
         if (e == null) {
             Collections.shuffle(objects);
             objectsWereRetrievedSuccessfully(objects);
         } else {
             objectRetrievalFailed();
         }
     }
 }


Source: stackoverflow