user9957027
user9957027

Reputation:

How to arrange order of data that i get from firebase firestore

I had post user data to Firebase Firestore and i had successfully retrieve data also now i want to make it in order last post to first in the post wall. enter code here

 //Snapshot listener help us to retrive the data in real time
        firebaseFirestore.collection("user_post").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                //check for any changes in document
                for (DocumentChange doc: documentSnapshots.getDocumentChanges()){

                    //check if document get added
                    if (doc.getType() == DocumentChange.Type.ADDED){

                        Wall_post wall_post = doc.getDocument().toObject(Wall_post.class);
                        wallPosts_list.add(wall_post);

                        wallpostRecyclerAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

Upvotes: 0

Views: 2470

Answers (1)

ayan chakraborty
ayan chakraborty

Reputation: 284

If you have a posted timestamp then you can use this in your code. Change the time_stamp to you own variable that you had.

firebaseFirestore = FirebaseFirestore.getInstance();
Query postOrder = firebaseFirestore.collection("user_post").orderBy("time_stamp",Query.Direction.DESCENDING);

//Snapshot listener help us to retrive the data in real time
postOrder.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        //check for any changes in document
        for (DocumentChange doc: documentSnapshots.getDocumentChanges()){
            //check if document get added
            if (doc.getType() == DocumentChange.Type.ADDED){
                Wall_post wall_post = doc.getDocument().toObject(Wall_post.class);
                wallPosts_list.add(wall_post);

                wallpostRecyclerAdapter.notifyDataSetChanged();
            }
        }     
    }
});

Upvotes: 2

Related Questions