Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!