Reputation: 67
I am retrieving data from Firestore and I want to add an order of retrieving it, so I have tried this code:
Query query = firebaseFirestore.collection("docs").orderBy("date", Query.Direction.DESCENDING/*ASCENDING*/);
FirestoreRecyclerOptions<download> docsFirestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<download>()
.setQuery(query, download.class)
.build();
In date, I am adding time and date in String type like
02:42:31 - 2020/08/01
by this, I want to make sure the newest post appears on the top of recycler view. it works well in this day but the next day if the time let's say
01:00:00 - 2020/08/02
It is will appear before the post that added the previous day. Is there a better way to ordering data from Firestore?
Upvotes: 2
Views: 207
Reputation: 138824
Is there a better way to ordering data from Firestore?
Sure it is. Instead of storing the time and date as a String, you can store it either as a Java Date, the Date being a Firestore supported data type or as a Firestore Timestamp object. Here is an answer that can help you achieve that:
Now, having the type of the date
property as Firestore Timestamp, your query will work perfectly fine.
However, if you insist to store the dates and times as Strings, the option that you have is to store them in a format that can be sorted lexicographical and chronological at the same time.
The ISO 8601 format looks like this:
20200802T131425
That's the current time I'm answering this question.
August 02, 2020 1:14:25 PM UTC
Upvotes: 2