0

I would like to fetch the users rank from by using the score that has been used in the database. I have the database as:

scores-> uid -> score

data image

Here in the image I would like to sort the data of score in descending order,assign ranks to them (1 being the highest scorer's rank, 2 being the second and so on) and retrieve the current user's rank.

I have found solutions which help in sorting the users but how do I set their rank and find the rank of the current user is the problem.

    private void getHighestScore() {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
    Query query = databaseReference.orderByChild("score").limitToLast(1);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            userlist = new ArrayList<String>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                User score = postSnapshot.getValue(User.class);
                Log.d("test", " values is " + score.getName() + " " + score.getScore());

                       userlist.add(score.getScore()+user.getName());


            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

The above code has been used to add data to a list in a sorted manner. The problem being how to set the rank of the current user in the application

firebase retrieve highest 100 score

query for getting top 10 highest scores

Community
  • 1
  • 1
Saransh Agarwal
  • 305
  • 5
  • 18

0 Answers0