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?