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

130
Views
JavaScript sort array item after specific array id

I am sorting an array of object using the date values inside each object. I now have an edge case, if thenoDate property is set to true, that object shall be sorted after the object with the id 2.

An object looks something like this:

{
    id: 1 // just simple ids 1...99
    date: ...
    noDate: true // true or false
}

Currently its sorted like this, but that does not provide the logic I need.

data.sort((a, b) => (!a.noDate - !b.noDate) || new Date(a.date) - new Date(b.date))
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could sort first to get all noDate: true at top of the array and then map a new array to get all top items after id: 2 object. This approach needs a finally

let
    data = [
        { id: 0, noDate: true },
        { id: 1, date: new Date('2021-10-30') },
        { id: 2, date: new Date('2020-10-30') },
        { id: 3, noDate: true },
        { id: 4, date: new Date('2021-09-30') },
        { id: 5, date: new Date('2020-09-15') },
        { id: 6, noDate: true }
    ];

data = data
    .sort((a, b) => !a.noDate - !b.noDate || a.date - b.date)
    .flatMap((a => o => {
        if (o.noDate) { a.push(o); return []; }
        if (o.id === 2) return [o, a];
        return o;
    })([]))
    .flat();

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

simply

list.sort((a, b) => a.noDate ? -1 : new Date(a.date) - new Date(b.date))

Or if you want to sort by two field first sortNumber and then sorting by date

list.sort((a, b) => a.sortNumber == b.sortNumber ? new Date(a.date) - new Date(b.date): a.sortNumber - b.sortNumber)
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!