0

How I can sort an array by the first value. Below is the array example

[
    [
        14th Aug,
        8th Aug,
    ],
    [
        8th Aug,
        22th Aug,
    ],
    [
        6th Aug,
        14th Aug,
    ] 
]

As result, I need to get a new array such as

[
   [
        6th Aug,
        14th Aug,
    ],
    [
        8th Aug,
        22th Aug,
    ],
    [
        14th Aug,
        8th Aug,
    ] 
]

So only the first value must be taken into account for sorting

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Ollie
  • 1
  • 1
    Are the values of your array arrays of strings or arrays of Dates? – Apollo79 May 02 '22 at 15:01
  • Can you please add another month to your sample and result (if applicable)? Could there be different years as well? – PM 77-1 May 02 '22 at 15:02
  • Values are JS Objects – Ollie May 02 '22 at 15:06
  • What have you tried and what isn't working? The same logic in [Sorting an array of objects by property values](https://stackoverflow.com/q/979256/328193) (or any example of custom sort logic in JavaScript) applies here. The only difference is that your "objects" being sorted are also arrays. But the overall logic is the same. Pass a custom comparer function to the sort operation. Can you show your attempt and demonstrate in what way it doesn't work as expected? – David May 02 '22 at 15:08
  • @Ollie: JS Objects could be (among others) of class `Date` and class `String`. So what do you have? – PM 77-1 May 02 '22 at 15:08
  • Does this answer your question? [Sorting an array of objects by property values](https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values) – StrayCatDBA May 04 '22 at 13:33

0 Answers0