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

267
Visualizações
JavaScript array.sort doesn't work on Array of Objects

For some reason I can't sort my Array of Objects using Sort Function:

FullHistory.sort(function(a, b){
return a.Date.localeCompare(b.Date);
});

When I use console.log() - It returns: -1/0/1, but the Array's Order is not changing at all

Evidence

The rest of my Code:

let FullHistory = [];

    CurrentTitle = $(data).find(".normal_header").text().replace(" Episode Details", "");
    
    $($(data).find('.spaceit_pad').get().reverse()).each(function () {
        FullHistory.push({ Title: CurrentTitle, Ep: $(this).text().replace("Ep ", "").split(',')[0], Date: $(this).append(":00").text().replace("Ep ", "").replace("watched", "").replace(" Remove", "").split(",")[1].replace("on ", "").replace("at ", "").replace("  ", "") });
    });

I tried: changing let to var, using if statements in Sort Function, changing types, using Lodash Lib.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your Date properties will be sorted as strings -- that is what localeCompare does. It doesn't look at those strings as representing dates. Since the date format used is not ISO, the lexical order of those strings does match the order you would expect for the dates.

So convert the strings to numerical epochs:

let FullHistory = [
    {Title: "Grand Blue", Ep: "1", Date: "02/19/2020 23:36:00" },
    {Title: "Darling in the FranXX", Ep: "1", Date: "03/04/2019 23:44:00" },
];

FullHistory.sort(function(a, b){
    return Date.parse(a.Date) - Date.parse(b.Date);
});

console.log(FullHistory);

about 4 years ago · Juan Pablo Isaza 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