0

I want to sort data by timestamp but I can't. I user this code:

reference = database.getReference("posts");
Query re = reference.orderByChild("timestamp").limitToFirst(20);
//Query re = search.orderByChild("timestamp");
re.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot ds : snapshot.getChildren()){
            for (DataSnapshot dataSnapshot : ds.getChildren()){
                System.out.println(dataSnapshot);
                DataReturn dataReturn = dataSnapshot.getValue(DataReturn.class);
                if( dataReturn.getStatus() != null &&         dataReturn.getStatus().equals("approved")){
                    list.add(dataReturn);
                }

            }

            //Collections.reverse(list);

            customAdapter.notifyDataSetChanged();
        }

        if (refresh){
            swipeRefreshLayout.setRefreshing(false);
            refresh = false;
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        error.toException();
    }
});

what I can do now?

my database url json

https://hello-writer-2db52-default-rtdb.firebaseio.com/

How can I sort data in this condition.

I don't get any error

RmTomal
  • 70
  • 1
  • 7
  • Please edit your question to show how the `reference` variable is initialized? Also: what isn't working about the code you shared? Is there an error message? – Frank van Puffelen Mar 12 '23 at 18:56
  • Firebase Realtime Database sorts/filters direct child nodes of the path you query on. It can't handle double nesting as you're trying to do here. You'll want to make a single flat list of posts, and add the user ID to each post. If you need to order/filter on both user Id and timestamp, see https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Mar 12 '23 at 19:38
  • hello i make that type of database now but not working – RmTomal Mar 13 '23 at 07:56
  • 1
    If you changed your data model and can't get it to work, you might want to post a new question with its own [minimal repro](http://stackoverflow.com/help/mcve) (read the link please, it's useful). – Frank van Puffelen Mar 13 '23 at 18:46

0 Answers0