-1

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.

Joji
  • 4,703
  • 7
  • 41
  • 86

5 Answers5

2

An alternative is to specify with extra arguments what the format is (regex) and how it should be reordered to ISO-style YYYYMMDD format. Then the function can perform that replacement and do a lexical sort:

function sortTime(array, regex, iso) {
    let fmt = time => time.replace(regex, iso);
    return array.sort((time1, time2) => 
        fmt(time1).localeCompare(fmt(time2)));
}

// demo
let dates = ['03-11-2018', '04-12-2018', '10-01-2017', '10-12-2017'];
let result = sortTime(dates, /(..)-(..)-(....)/, "$3$2$1");
console.log(result);

So if the input should be interpreted as mm-dd-yyyy instead of dd-mm-yyyy, then make the third argument "$3$1$2" instead of "$3$2$1".

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Hey thanks for the answer! is `localeCompare` necessary here? I haven't used it before – Joji Oct 08 '21 at 21:22
  • It is the way to compare strings and get an integer result (-1, 0, 1). There are other ways of course. If the `iso` parameter guarantees that the replacement will only consist of digits and could be evaluated as number for comparison, then you can also do `fmt(time1)-fmt(time2)`. – trincot Oct 08 '21 at 21:23
0

It's a little simpler using subtraction rather than nested ternaries.

function sortTimestamp(array) {
  array.sort((time1, time2) => {
    const [day1, month1, year1] = time1.split('-')
    const [day2, month2, year2] = time2.split('-')
    return year1 - year2 || month1 - month2 || day1 - day2;
  })
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can first convert them to Date objects then use the getTime functionality to compare for sorting. A similar question was answered here

const dates = ['03-11-2018', '04-12-2018', '10-01-2017', '10-21-2017']

sortedDates = dates.sort((a,b)=>{
  const dateA = new Date(a)
  const dateB = new Date(b)
  //change the comparison sign for required orde
  return dateA.getTime() < dateB.getTime() 
  ? 1 
  : -1
 
})
console.log(sortedDates)
Craques
  • 953
  • 1
  • 8
  • 16
0

Similar to Craques answer, but added both ASC, DESC sorting using the getTime method

ASC:

dates.sort((a,b) => new Date(a).getTime() - new Date(b).getTime())

DESC:

dates.sort((a,b) => new Date(b).getTime() - new Date(a).getTime())
Obaida Alhassan
  • 516
  • 1
  • 5
  • 12
0

It turns out that the new Date() constructor is fairly robust when it comes to interprete date strings. The formats used in the following snippet seem to work reliably:

const a=["2018-09-05","09-21-2018","12/24/2018","19 Sep 2018",'03-11-2018', '04-12-2018', '10-01-2017', '10-21-2017'];

console.log(
  a.map((s,i)=>[new Date(s),i])
   .sort(([a],[b])=>a-b)
   .map(([_,i])=>i+': '+a[i])
)
   

You can leave out the index (i+': '+) in the output of course to make it look pretty again.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43