Home > Enterprise >  Sort firebase Recycleview real-time database base on post with most likes in last 24 hours
Sort firebase Recycleview real-time database base on post with most likes in last 24 hours

Time:04-08

How can I sort Recycleview base on post that has the most likes within the last 24 hours, I am currently storing likes in my real-time firebase database here is image for the structure of my databaseimage-databse

CodePudding user response:

You probably don't mean sort recycler-view. You would probably be better off sorting the data in your realtime database call by likes and date then your recycler-view would be built in that same order. See more info and examples in the Docs

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

You can also filter the call via startAt(), startAfter(), endAt(), endBefore(), and equalTo() etc etc. Hope this points you in the right direction.

  • Related