0

I want to list out the newest order until the oldest order. I have set the date format before the users do payments.

// Get current date & time
val currentDateTime = LocalDateTime.now()
// Format date time style
currentDateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))
// Finalize convert format
val date = DateTimeFormatter.ofPattern("dd MMM yyyy, HH:mm").format(currentDateTime)

After the users do the payment, it will add to my Firestore. When I retrieve the data, it's only descending my date of the month.

    fun getOrderList(activity: Fragment, recyclerView: RecyclerView) {
        mFirestore.collection(Constants.ORDERS)
            .orderBy("date", Query.Direction.DESCENDING)
            .get()
            .addOnSuccessListener { result ->

                val orderItem = result.toObjects(UserOrderList::class.java)
                recyclerView.adapter = OrderListItemAdapter(activity, activity.requireContext(), orderItem)
            }
    }

Firestore data structure

My date output like 06 Jun 2021, 10:50 -> 05 Nov 2021, 14:22 -> 03 May 2021, 10:58 -> 03 Apr 2021, 09:13

Date image result

How can I get sort the date from newest until oldest and list it out into my RecyclerView?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Tea
  • 5
  • 4

2 Answers2

1

You aren't getting the desired results because the date field inside the document is a String, and when you order strings, the order is lexicographical.

To solve this, you have to change the type of the field to be a Firestore Timestamp, and then your query will work as expected.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Is it still available time format if I use timestamp? – Tea Nov 08 '21 at 12:22
  • Yes, it is. A [Timestamp](https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp) object contains a time component with nanosecond precision. – Alex Mamo Nov 08 '21 at 12:42
  • Can you teach me how to write? I'm still new not so familiar and don't understand how to code date format. – Tea Nov 08 '21 at 12:52
  • You should make your own attempt given the information in the answer and ask another question if something else comes up. – Alex Mamo Nov 08 '21 at 13:15
  • I tried to set DateTimeFormatter in my adapter, but it doesn't works and get the Required: TemporalAccessor. val date = item.date!!.toDate() val formattedDate = DateTimeFormatter.ofPattern("dd MMM yyyy, HH:mm").format(date) – Tea Nov 08 '21 at 15:00
  • "doesn't work" doesn't provide enough information so we can help. So please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Nov 08 '21 at 15:08
  • But remember, you should use a Timestamp object as explained in that [answer](https://stackoverflow.com/questions/48474957/how-to-add-a-timestamp-in-firestore-with-android/48475027#48475027). – Alex Mamo Nov 08 '21 at 15:09
0

I would add one more field in the Firestore, which accepts Unix time in milliseconds. Let's call order_created_date. Then while retrieving the list of the Orders, you can use the same orderBy("order_created_date",Query.Direction.DESCENDING) method. In that case it will compare numbers.

I would also suggest using this approach of getting current time

val currentDateTime = Calendar.getInstance()
currentDateTime.timeInMillis

As your method LocalDateTime.now() requires API level 26 or above.

Regards.

Azamat Mahkamov
  • 912
  • 14
  • 18
  • Is it available to format date? I'm still new not familiar in date – Tea Nov 08 '21 at 12:38
  • Yes you can format Unix time into any Date format you want. Also, I agree what @Alex explained. His approach is almost the same as this one. The comparison should not be with Strings) – Azamat Mahkamov Nov 09 '21 at 03:45