4

New at Java programming and couldn't find an answer for this.

I created a Firebase Database for a small game I built and managed to get the scores to be stored correctly. Now I want to build the leaderboard with the top ten scores. The database itself is indexed by top scores, and I get the top 10 scores with:

mDatabase.child(gameName).child("Score").limitToFirst(10).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user value
            HashMap<String,Long> topScores = (HashMap<String, Long>) dataSnapshot.getValue();
....

The problem with that is that (from what I can see) what I receive is a HashMap which I store in topScores. Again, from what I understand so far, HashMaps are unorganized, so when you print it, it looks something like this:

{DummyData1=0, try=0, dummy2=5, lalala=10, Name1=0, try2=0, lala=11, try3=0, la=3, dummy=1}

So, no discernible order.

Finally, the question is how would you recommend I go about ordering the HashMap created in order to store the Key and Value pairs in their corresponding TextView (the top scorer and top score in the first Row of TextViews, and so on).

As a reference, how I plan on showcasing the leaderboard

Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

7

You're doing two things wrong:

  1. you're not telling Firebase to order the data by any value
  2. you're converting the results to a Map, which is inherently unordered

Assuming that you have this JSON structure under Score (please post the JSON as text next time you ask a question):

Score

The code would be:

mDatabase.child(gameName).child("Score")
         .orderByValue().limitToLast(10).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            System.out.println(child.getKey()+"="+child.getValue(Long.class));
        }

And it will print:

Puf=30

Polarbear0106=42

KatoWulf=60

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hey Frank. Thanks for the reply. When I add ".orderByValue()" it brings the ten lowest scores, not highest. Any idea as to why? Also, `DataSnapshot child in dataSnapshot.getChildren()`is getting some parsing errors in Android Studio. Is there something missing? – Mariano Molina Jul 17 '16 at 16:29
  • 1
    Yeah, you'll want `limitToLast(10)`, since ordering is *always* ascending in Firebase. The `in` is indeed a typo. I've fixed both in the answer, but recommend you spend some time on the [documentation on sorting and filtering](https://firebase.google.com/docs/database/android/retrieve-data#sort_data). – Frank van Puffelen Jul 17 '16 at 17:00
  • Great. Thanks Frank! – Mariano Molina Jul 17 '16 at 17:13
  • Since I am new, it doesn't record my upvotes unfortunately. Sorry! But thanks! – Mariano Molina Jul 18 '16 at 23:52
  • 1
    You should be able to accept the answer by checking the mark to the left. This is the more important actions, because it shows others that your question has been answered. – Frank van Puffelen Jul 19 '16 at 00:44
  • 1
    Maybe this is not kosher, but came back to check this old thread and now I can give up votes so there, have a couple. =) – Mariano Molina Jan 03 '22 at 19:24
  • lol, thanks Mariano --- Just don't go overboard, as Stack Overflow checks for (what are called) serial upvotes and reverts them, which is a sad experience for every helper that happens to. ;-) – Frank van Puffelen Jan 03 '22 at 19:34