0

I want order Firebase by datetime(userOrderDateTime) and appear that in adapter with this order.
Check my Firebase image
enter image description here
My code is (I try below but it isn't work):

ref.orderByChild("userOrderDateTime").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    UserOrder userOrder = dataSnapshot.getValue(UserOrder.class);
                    list.add(userOrder);
                }

                adapter.notifyDataSetChanged();

                if (list.size() == 0) {
                    gifImageView.setVisibility(GifImageView.VISIBLE);
                    userOrderBlankText.setVisibility(View.VISIBLE);
                    btnDeleteUserOrderItems.setVisibility(Button.INVISIBLE);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
usern
  • 9
  • 4

1 Answers1

0

As I see in your screenshot, your userOrderDateTime property is o type String, meaning that the order will always be lexicographically. To have a correct order, you should store in that field a timestamp value, rather than a String. To achieve that, please see my answer from the following post:

Once you change the type of the field, your elements will be ordered correctly.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193