DEV Community

Cover image for Sorting in descending order in Firebase Realtime Database
Siril M Philip
Siril M Philip

Posted on

Sorting in descending order in Firebase Realtime Database

In my case, upload images to Firebase storage and store the download url in realtime database. After fetch the url images display in ascending order that means which one I upload in first that one show in first on my website. But I want to display the latest uploaded image in first on my website then I search lot of websites, youTube videos ...
Finally I got the answer : Firebase doesn't allow returning by descending order.
But I am not ready to give up.
After a night I got the solutions,

I add new field like timeStamp and change timeStamp to negative number

image1

then upload both url and negative timeStamp

image2

then fetch the data like 👇 from firebase :

image3

then the images or videos display in descending order of negative timeStamp.

Top comments (0)