Order Firestore data by TimeStamp in Ascending order
You have to also add index in your firebase account which is in database -> Indexes -> add index
You cannot use a String (timeStamp
) when querying your database instead of a Date (date
) and expect to behave as it was a date. So to solve this, please change the following line of code:
firestoreDb.collection("ideas")
.orderBy("timeStamp", Query.Direction.ASCENDING)
to
firestoreDb.collection("ideas")
.orderBy("date", Query.Direction.ASCENDING)
To make it work, this kind of query requires an index. To create one, please check my answer from the following post:
- Firestore whereEqualTo, orderBy and limit(1) not working
In my case, the vanilla version would be,
firestoreDb.collection("ideas")
.orderBy("timestamp", "asc")
firestoreDb.collection("ideas")
.orderBy("timestamp", "desc")
"ideas" is the name of your collection
"timestamp" is the key or field or column name to be used for sorting.
"asc" or "desc" is the option to be used for the order