Issue
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)
}
}
My date output like 06 Jun 2021, 10:50 -> 05 Nov 2021, 14:22 -> 03 May 2021, 10:58 -> 03 Apr 2021, 09:13
How can I get sort the date from newest until oldest and list it out into my RecyclerView?
Solution
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.
Answered By - Alex Mamo