1

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))
4
  • perhaps data.sort((a, b) => a.noDate ? -1 : new Date(a.date) - new Date(b.date) ) Commented Nov 2, 2021 at 19:01
  • @mplungjan I want to sort the object with the noDate value set to true to be sorted after the object with id 2 Commented Nov 2, 2021 at 19:11
  • do you have some example data? Commented Nov 2, 2021 at 19:12
  • @NinaScholz I created a snippet here, hope it is more understandable now :) kopy.io/N6q9Y Commented Nov 2, 2021 at 19:39

2 Answers 2

1

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; }

Sign up to request clarification or add additional context in comments.

Comments

1

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)

1 Comment

I want to sort the object with the noDate value set to true to be sorted after the object with id 2, does this actually solve this or am I understanding something wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.