Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
Sorting array by date

Hello everyone. I want it to be sorted by date and rerender when clicking Recent. But I just couldn't. When I console.log I see the array as unsorted. and when I do console.log I get undefined. It feels like I'm missing something very simple but I couldn't figure it out. Where am i missing? Why console.log returns undefined ? Any help?

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

I have an idea for what you can do - arrange the date format in reverse (yyyy-mm-dd) then remove the - you will have a sortable number that you can use to sort the array.

about 4 years ago · Santiago Gelvez Relatório

0

1 convert to an array with JSON.parse() if the values are in JSON ... They not in your case pure array

2 Create a function and compare the dates using new Date()

  var arr = [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 
                08:00:00 AM}];
 
 sortByDate(arr) {
      arr.sort(function(a,b){
      return Number(new Date(a.readableDate)) - Number(newDate(b.readableDate));
      });

return arr;
}
about 4 years ago · Santiago Gelvez Relatório

0

Let's get to the error. First things first, the reason why you are getting undefined is you are having allFeeds object with feeds as an array. When you console.log(allFeeds.date) there is no key-value pair date in your allFeeds object, so it gives you undefined and when you do console.log(allFeeds.feeds.date) here you should use console.log(allFeeds.feeds[0].date) because allFeeds.feeds is an array.

To solve your problem, you can use the below solution.

a.sort((x,y)=>{return new Date(x.date.split("-").reverse().join("-")).getTime() - new Date(y.date.split("-").reverse().join("-")).getTime()})

We are doing this because the Date(dateString) recieves dateString in format yyyy-mm-dd and you are having date in dd-mm-yyyy which is not acceptable, also we are converting it to Date object because as a string dd-mm-yyyy or yyyy-mm-dd does not make much sense, after converting it to the Date object we can use its methods to use the data better.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda