0

I wanna create a global ranking of people. These people are saved in the Realtime Database on Firebase. I need to display these characters on a list and I need them to be sorted from highest to lowest.

                mAuth = FirebaseAuth.getInstance();
        DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
        Query top10Query = usersRef.orderByChild("lvl").limitToLast(4);

        if (mAuth.getCurrentUser() != null) {
            top10Query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        Log.i("Firebase", String.valueOf(userSnapshot.child("dati").child("name").getValue()));
                        Log.i("Firebase", String.valueOf(userSnapshot.child("statistiche").child("lvl").getValue()));

                        String stringlvl = (String) userSnapshot.child("statistiche").child("lvl").getValue().toString();
                        String stringname = (String) userSnapshot.child("dati").child("name").getValue().toString();
                        String stringlvl = userSnapshot.getValue().toString();

                        myArrayList.add(0, stringlvl);
                        myArrayAdapter.notifyDataSetChanged();

                        Arrays.sort(new ArrayList[]{myArrayList}, Collections.reverseOrder());
                        reverse(myArrayList);

                        for (int i = 0; i<myArrayList.size(); i++){
                            arrayText[i].setText(myArrayList.get(i));
                        }

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            });
        }
public class Statistic {
    int lvl;

    public Statistic() {
    }

    public Statistic(int lvl) {
        this.lvl = lvl;
    }

    public int getLvl() {
        return lvl;
    }

    public void setLvl(int lvl) {
        this.lvl = lvl;
    }
}

This is my statistic.Class

enter image description here

This is my Realtime Database:

The problem I have right now is that it doesn't sort the values I have in my database in descending order.

Some advice?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi Luca Leone, can you shaer your database? and can you share your Statistic.class ? You can edit your answer – Ticherhaz FreePalestine Mar 21 '22 at 18:17
  • Thank you for your answer. I searched around for a while and managed to go a little further. This is what I got. By making logs I can print the values I want from my database, the problem is that I don't print them in the order I want. The order in which he puts them is the same that he sees from the database starting from the bottom. –  Mar 21 '22 at 22:18
  • 1
    I see you are the doing at the wrong way. If you want a fast solution. you need to remove `statistiche`. place the `lvl at the root level same as `dati`. – Ticherhaz FreePalestine Mar 22 '22 at 00:17

1 Answers1

1

As I see in your schema, there is an inconsistency, meaning that the children under "users", have different types of names. One is "A0", others are UIDs and others are specific names like "giovanni". It's best to have all children follow the same rule.

Your query doesn't work the way you expect because you are missing a child from your reference. To be able to get accurate results, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = db.child("users");
Query queryByLevel = usersRef.orderByChild("statistiche/lvl").limitToLast(4)
queryByLevel.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            for (DataSnapshot ds : snapshot.getChildren()) {
                String name = ds.child("statistiche").child("name").getValue(String.class);
                Log.d("TAG", name);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result of the above code will be as follow in ascending order:

A0
carla
lucio
giovanni

If you however need a descending order, please see my answer from the following posts:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • thanks for your answer. I tried to implement the code you sent me. But it gives me a problem, he sees the if, but he skips it and goes straight to the else. –  Mar 22 '22 at 16:21
  • I'm not aware of any if statement inside the provided code? Does the code work in the way I wrote it? – Alex Mamo Mar 22 '22 at 20:40
  • Hi Alex, I managed to solve it and everything is clear to me, thanks anyway for the available –  Apr 06 '22 at 06:40