Sort data by date in Firebase

0

I would like to sort the data returned from Firebase by the Expiration Date, I am currently doing the following:

this.Collection = document.collection('values', ref => ref.orderBy('data_vencimento'));

But as can be seen in the image below, you are not ordering correctly, are there other ways to sort by date? Can I do a sort () on Observable returned from Firebase?

    
asked by anonymous 13.03.2018 / 17:03

2 answers

0

In fact you are sorting correctly, but you will not get the desired result because you are sorting in a string any not as a date in pt-BR format, if you change the format to yyyy-mm-dd , you have the desired result. p>

I suggest that I use the American standard in the database and mask the dates on the front end, for example:

var data = new Data //Retorna no formato Tue Mar 13 2018 15:23:51 GMT-0300 (-03)
data.toLocaleDateString() //Retorna no formato 13/03/2018
    
13.03.2018 / 19:25
0

Dude the idea would be to save the date in timestamp in a new property eg:

const data = new Date();

const meuObjeto = {
    data: data.toLocaleDateString(),
    timestamp: data.getTime()
};

So you can compare both time differences and ordering etc ...

    
23.04.2018 / 19:00