Reputation: 83
I have the code:
listView = (ListView) findViewById(R.id.list);
list = new ArrayList<String>();
arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
leaderBoard();
}
FirebaseAuth auth = FirebaseAuth.getInstance();
public void leaderBoard(){
database.getReference().child("db/auth/user/").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
list = new ArrayList<String>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
System.out.println("The score is: " + snapshot.child("score").getValue());
String s = snapshot.child("name").getValue() + " " + snapshot.child("score").getValue();
list.add(s);
arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
My goal is to show a list with the users' names and score, so far so good, as shown in the photo.
But now I need the list to show users and points in increasing order based on the score. Can someone help me?
Upvotes: 2
Views: 460
Reputation: 599946
To order the children by the value of their score
property, you can do:
database.getReference().child("db/auth/user/").orderByChild("score").addValueEventListener(new ValueEventListener() {
...
As far as I can see, the rest of your code can remain the same.
Also see the Firebase documentation on ordering and filtering data.
Upvotes: 2