Sort by date how much they are of the string type

Asked

Viewed 895 times

2

i am developing an application in Engineering which receives the following object from an API:

pessoas = [
    { nome: 'Lucas', data: '2010-Fev-04'},
    { nome: 'Felipe', data: '1994-Dec-10'},
    { nome: 'Ana', data: '1994-Jun-21'},
    { nome: 'Carlos', data: '1991-Fev-20'}
];

I need to sort the column by date, but it comes in string form from the API. I can’t see a way to sort the dates correctly since I have no way to change the content of this object and make the date a timestamp.

  • From the moment you receive this object from the API, it is only a copy of the original data and could be manipulated in your Angular application. I didn’t understand the restriction you put, could give more context?

  • Hi Marcel. The problem is that with the date in the form of a string, the ordering will not take place in a temporal way. For example, 1994-Dec-10 would be less than 1994-Jun-21. I thought of creating a second object from the first, using the new Date to change the format, but as the size of the object may vary, I don’t know if this would be the best option

2 answers

5

With momentjs to make the date Parsing. https://momentjs.com

The dates must have the same language, in the example in question Feb,Dec || Feb,Dec in your case are mixed.

pessoas = [{
        nome: 'Lucas',
        data: '2010-Fev-04'
    },
    {
        nome: 'Felipe',
        data: '1994-Dez-10'
    },
    {
        nome: 'Ana',
        data: '1994-Jun-21'
    },
    {
        nome: 'Carlos',
        data: '1991-Fev-20'
    }
];


moment.locale('pt')

function comparar_datas(a, b) {
    d1 = moment(a.data, "YYYY-MMM-DD");
    d2 = moment(b.data, "YYYY-MMM-DD");
    if (d1.isAfter(d2)) {
        return 1;
    } else if (d1.isBefore(d2)) {
        return -1;
    }
    return 0;
}

pessoas.sort(comparar_datas);
console.log(pessoas)
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>

1


I made a way without using additional resources. It will generate a new array called ordenados with objects ordered by date using .sort(). The logic is to convert the dates into valid date object. With this one can make a direct comparison, checking if one is larger than the other (see explanations in the code):

function fData(i){
   
   // objeto para converter nomes em números dos meses
   var mes = {
      Jan: 0, Fev: 1, Mar: 2, Abr: 3, Mai: 4, Jun: 5,
      Jul: 6, Ago: 7, Set: 8, Out: 9, Nov: 10, Dez: 11
   }
   
   // converto a data recebida em array
   i = i.data.split("-");
   
   // converto os valores em objeto data
   var d = new Date(i[0],mes[i[1]],i[2]);
   
   // retorna o objeto
   return d;
   
}

pessoas = [
    { nome: 'Lucas', data: '2010-Fev-04'},
    { nome: 'Felipe', data: '1994-Dez-10'},
    { nome: 'Carlos', data: '1991-Fev-20'},
    { nome: 'Jose', data: '1994-Jun-21'},
    { nome: 'Maria', data: '1991-Fev-19'},
    { nome: 'João', data: '1984-Mar-01'}
];

var ordenados = pessoas.sort(function(a,b){

   // retorna os valores da função fData()
   // em formato de objeto de data
   a = fData(a);
   b = fData(b);
   
   // retorna primeiro o que for menor
   // caso queria inverter, basta trocar ">" por "<"
   return a > b;

});

console.log(ordenados);

  • 1

    Thank you! You helped a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.