2

I have the following structure

Each array has the following data:

{{ id: 39, treaty_number: "qwe", insurant_name: "222", belonging_to_the_holding_company: "test", date_start: "2016-04-15", etc }}

How do I sort each array in ascending order date_start?
I am writing on Angular 4 and typescript v.2.4.1. It is possible and on pure JS. Thank you!

1
  • Why not just a standard javascript sort? .sort((a, b) => { if (a.date_start < b.date_start) { return -1; } if (a.date_start > b.date_start) { return 1; } return 0; }); Commented Oct 8, 2017 at 15:43

2 Answers 2

2

While you have ISO 8601 dates, you could sort it with String#localeCompare.

data.forEach(function (array) {
    array.sort(function (a, b) {
        return a.date_start.localeCompare(b.date_start);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this:

jsonData.forEach(eachArray => {
   eachArray.sort( (aob, bob) => {
         if( aob.date_start < bob.date_start ) {
           return 1;
         } else if(aob.date_start > bob.date_start ) {
             return -1;
         }
         return 0;
   });
});

Comments

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.