Reputation:
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
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?
Upvotes: 0
Views: 135
Reputation: 139019
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:
Sort data to RecyclerView based on latest date from Firebase
how to sort the key value of a map entry in descending order?
Upvotes: 1