0

I want to get highest scores in leaderboard. Here is my database:

Database

Here is my rules:

Rules

And here is my code:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference newRef = database.getReference("tr/scores");
newRef.orderByChild("skor").limitToLast(3).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Gson gson = new Gson();
            String value = gson.toJson(dataSnapshot.getValue());
            Log.d("Debug",value);
        }
        @Override
        public void onCancelled(DatabaseError firebaseError) {
            Log.e("Debug","Error at firebase response.");
        }
    });

This returns 3 childs but not ordered by "skor" variable. I read the docs but the example in docs is:

{
  "scores": {
   "bruhathkayosaurus" : 55,
   "lambeosaurus" : 21,
   "linhenykus" : 80,
   "pterodactyl" : 93,
   "stegosaurus" : 5,
   "triceratops" : 22
  }
}

But my database has dynamic keys for each childs. Any idea?

easeccy
  • 4,248
  • 1
  • 21
  • 37
  • Try newRef.orderByChild("skor").orderByValue().limitToLast(3).addListenerForSingleValueEvent... – Rahul Mar 29 '17 at 17:37
  • It gives runtime error `java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!` – easeccy Mar 29 '17 at 17:39
  • Oh yes, multiple orderby will throw error, We have alternative for that. Can't we add comparator in our java object? – Rahul Mar 29 '17 at 17:41
  • What do you mean by comparator? – easeccy Mar 29 '17 at 18:00
  • I'm guessing that you're not handling the value correctly. Please share the minimal code in the `onDataChange` method that reproduces the problem. – Frank van Puffelen Mar 29 '17 at 18:00
  • http://stackoverflow.com/questions/4108604/java-comparable-vs-comparator – Rahul Mar 29 '17 at 18:01
  • @FrankvanPuffelen I edited the java code. I think the problem is on my firebase rules. – easeccy Mar 29 '17 at 18:08
  • Nope, those have no influence on this. The problem is that you throw the order away in your `onDataChange()`. By definition children in JSON are unordered. So when you convert the snapshot to JSON, you lose ordering. You need to loop over the snapshot: `for (DataSnapshot child: dataSnapshot.getChildren()) {...`. See http://stackoverflow.com/questions/40068338/firebase-getvalue-not-in-the-same-order-as-database/40068496#40068496 – Frank van Puffelen Mar 29 '17 at 18:17
  • @Nobody TRY this newRef.orderByChild("skor").limitToFirst(3). – Deepmala singh M Mar 30 '17 at 07:06
  • @FrankvanPuffelen it worked but sorted by alphabetically. 99 comes first that 100. How can I fix it? – easeccy Mar 30 '17 at 07:58
  • That means you've stored the numbers as strings. Solutions are to either store them as actual numbers (without the quotes around it), or to pad the numbers with zeroes so they're all the same length (since then `099` will comes before `100`). – Frank van Puffelen Mar 30 '17 at 13:25
  • @FrankvanPuffelen that info saved my day. Big thanks :) We'll also planning a big event and use firebase as backend. Can I send u an e-mail about it? (Is firebase suitable for it etc.) – easeccy Mar 30 '17 at 13:37
  • If you need personalized help, [reach out to Firebase support](https://firebase.google.com/support/contact/troubleshooting/). Otherwise: Stack Overflow or our firebase-talk mailing list are the best places to get answers. – Frank van Puffelen Mar 30 '17 at 13:46

0 Answers0