简体   繁体   中英

Firebase orderByKey doesn't work

I have an object.

{
featured-routines: {
 1465895195: "-LALALA"
 1465898164: "-KK7pEednXoUBszpqCmg"
 1465898185: "-KK9xFyA8skOvSLhaHWq"  
}
}

and I try to get them by calling:

Database.getFeaturedReference()
        .orderByKey()
        .addValueEventListener(routineListener);

The result should be in ascending order, but it's not. It's totally random. It isn't even in the same order as it shows in Dashboard GUI:

{ key = featured-routines, value = {1465898185=-KK9xFyA8skOvSLhaHWq, 1465895195=-LALALA, 1465898164=-KK7pEednXoUBszpqCmg} }

Anyone knows how to get them in the right order?

To handle the children in the order you request them, you have to either use a ChildEvenListener or DataSnapshot.getChildren() within your ValueEventListener :

    ValueEventListener routineListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot child : snapshot.getChildren()) {
                Systme.out.println(snapshot.getKey());
            }
        }
        ...
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM