0

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?

Manuel
  • 14,274
  • 6
  • 57
  • 130

3 Answers3

1

Why not just randomize the data after you query?

JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • Can definitely do that! I was just wondering if I had missed something in the documentation and that there was some secret built-in function to do this. Oh well. – drearypanoramic Oct 17 '15 at 17:38
1

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();
         }
     }
 }
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
0

I guess that was pretty easy... I hope others find this helpful!

            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "SuggestedUser");

                ob = query.find();

                // Randomizes the order of the records in the query
                Collections.shuffle(ob);

                for (ParseObject author : ob) {
                    ParseFile image = (ParseFile) author.get("brandImage");
                    SuggestedUser map = new SuggestedUser();
                    map.setRank((String) author.get("author"));
                    map.setUsername((String) author.get("username"));
                    map.setFlag(image.getUrl());
                    map.setUserID((String) author.get("userId"));
                    worldpopulationlist.add(map);