I have an array of timestamps or strings that are of this format - day–month–year.
e.g
['03-11-2018', '04-12-2018', '10-01-2017', '10-12-2017']
And I want to sort them chronologically so the above array would become [ '10-01-2017', '10-12-2017', '03-11-2018', '04-12-2018' ]
I am aware that you can use moment.js to do this but I want to write this by hand without using any 3rd lib.
Here is my attempt:
function sortTimestamp(array) {
  array.sort((time1, time2) => {
    const [day1, month1, year1] = time1.split('-')
    const [day2, month2, year2] = time2.split('-')
    return year2 > year1
      ? -1
      : year2 < year1
      ? 1
      : month2 > month1
      ? -1
      : month2 > month1
      ? 1
      : day2 > day1
      ? -1
      : 1
  })
}
It works ok but it is not really readable and it is a very imperative approach. I wonder if there is a better way to do it and also ideally it can be extended to support that other date formats e.g. month–day–year.
 
     
     
     
     
    