0

I understand that all data in Firebase is printed in ascending order.

I am trying to create a leaderboard based on scores but can't figure out how to flip the leaderboard into descending order can anyone help here?

Code:

database = FirebaseDatabase.getInstance().getReference().child("Users");
listview = (ListView) findViewById(R.id.Leaderboard);
Collections.sort(leaderboardlist, Collections.reverseOrder());
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,leaderboardlist);

listview.setAdapter(adapter);

Query queryRef = database.orderByChild("score").limitToLast(10);
queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        String user = dataSnapshot.getValue(User.class).toString();
        leaderboardlist.add(user);
        adapter.notifyDataSetChanged();
    }
});
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54

1 Answers1

1

As I remember it, you are correct in Firebase data is in ascending order.

You will need to order the 'leaderboardlist' in the application. Collections.reverse(leaderboardlist); is what you are looking for.

After a child has been added, the item will need to be inserted in the correct index.

Svaerd
  • 19
  • 3
  • I have tried that but no joy? Is there a certain place I should have this at the moment I have placed it just before where I declare the array adapter – Chris Kavanagh Nov 28 '19 at 11:44
  • I believe your issue is that the sorting does not trigger on update. While the initial list would be sorted the new elements would not adhere to the same sorting-rules. However, you probably would not like to re-sort the list on each update. You have a sorted list, so inserting in the correct index should be triggered at each update. https://stackoverflow.com/questions/16764007/insert-into-an-already-sorted-list might help you figure out how to do this. – Svaerd Nov 29 '19 at 12:18