0

In my Firebase Firestore, I have a large number of documents in a collection, each of which has a field called bookTime, which stores a date in String format. The format is dd MM yyyy HH, so we can't sort it in descending order to get a meaningful list with the latest document on top. The format we need for it to work properly is yyyy MM dd HH. This is my query:

mQuery = mQuery.orderBy("bookTime", Query.Direction.DESCENDING);

What change should I make so I can actually get a list with the document having the newest bookTime on top?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Shlok Jhawar
  • 277
  • 2
  • 3
  • 19

1 Answers1

2

What change should I make so I can actually get a list with the document having newest bookTime on top?

First of all, don't use Strings to store dates. It's recommended to use Date objects. The Date is a Firestore supported data type.

For more information on how to store a Date, please see my answer from the following post:

Once you store the Date correctly, that 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:

20200329T184525

That's the current time I'm answering this question.

March 29, 2020 6:45:25 PM UTC
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193