Shyngys Rakhad
Shyngys Rakhad

Reputation: 196

orderByValue in child [Firebase Database Android]

This is my firebase database

enter image description here

I need order this list by value of the child "total" I try to use this code

data.child("rating_apps").child("Developing").orderByChild("total").orderByValue().addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                 list.add(dataSnapshot.child("name").getValue(String.class));
                 adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Is it possible to order the list by value which is not close from parents?

Upvotes: 0

Views: 8208

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

You can only have one orderBy... in a query. Your orderByValue()causes Firebase to order on the value of the wrong property (the keys iirc).

If you remove orderByValue() (and thus just have orderByChild("status")), Firebase will on the value of the status property of each child.

As you noted, it will indeed sort those values as strings, since you stored your numeric values as strings. If you want them sorted numerically, store them as actual numbers.

Upvotes: 2

MJM
MJM

Reputation: 5311

Query myTopPostsQuery = databaseReference.child("user-posts").child(myUserId)
        .orderByChild("starCount");
myTopPostsQuery.addChildEventListener(new ChildEventListener() {
    // TODO: implement the ChildEventListener methods as documented above
    // ...
});

Upvotes: 1

Related Questions