0

I'm using a Firebase database. My "Users" data looks something like this:

Users {
  "38NFjDwCPAdkdvbzLb1VkFeaBIV2" : {
    "points" : 10,
    "userEmail" : "abc@hotmail.com",
    "userName" : "Tom"
  },
  "pH3HWzu9SVbW56jwqCb55hHryAD2" : {
    "points" : 17,
    "userEmail" : "xyz@hotmail.com",
    "userName" : "Jerry"
  }
}

Now I want to display Users according to their points, in descending order. I can use orderByChild("points") but that only displays the list in an ascending order.

This is what I'm doing right now:

public class Leaderboards extends AppCompatActivity {

    private ListView leaderboardsList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaderboards);

        leaderboardsList = (ListView) findViewById(R.id.leaderboardList);

        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

        DatabaseReference userRef = rootRef.child("Users");

        DatabaseReference carsRef = rootRef.child("Card");
        Query qUserRef = userRef.orderByChild("points");

        FirebaseListAdapter<User_DB> firebaseListAdapter = new FirebaseListAdapter<User_DB>(this, User_DB.class, android.R.layout.simple_list_item_1, qUserRef ) {
            @Override
            protected void populateView(View v, User_DB model, int position) {

                String points = String.valueOf(model.getPoints());
                ((TextView)v.findViewById(android.R.id.text1)).setText(points);

            }
        };

        leaderboardsList.setAdapter(firebaseListAdapter);
    }

}

What changes do I make to the above code in order to display my list in descending order, according to each user's points?

AL.
  • 36,815
  • 10
  • 142
  • 281
  • There is no built-in way get the results in descending order. The common solutions are to keep an extra property with the inverted value (i.e. `negativeScore`) or to do the inversion client side (given that the list is displayed on a mobile device, it should never be very long). See http://stackoverflow.com/questions/34156996/firebase-data-desc-sorting-in-android – Frank van Puffelen Apr 26 '17 at 03:37
  • Hey thanks for the idea. I'll try doing what you said about making another attribute with a negative score. – city explorer Apr 26 '17 at 09:52

1 Answers1

-1

You need to set priority for each your data and use orderByPriority function.
Because on Firebase, we cannot set descending or ascending to sorting.

wakwak3125
  • 34
  • 5
  • What about `orderByChild`? – OneCricketeer Apr 26 '17 at 01:57
  • `orderByChild` is always sort by ascending. – wakwak3125 Apr 26 '17 at 03:11
  • 1
    All ordering in Firebase is ascending, no matter if you `orderByKey`, `orderByValue`, `orderByChild`, or `orderByPriority()`. See https://firebase.google.com/docs/database/android/lists-of-data#data-order. There is no reason to use `orderByPriority()` anymore. See http://stackoverflow.com/a/31578297/209103 – Frank van Puffelen Apr 26 '17 at 03:34
  • I was thinking maybe I could store all this data in an array list and then sort it and display it in my list View. Does someone know how I could do that? – city explorer Apr 26 '17 at 08:45