Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda