Date Hell

Hola @Manu.n

it’s my code so far to sort and return a Date array (ascending) when a user has the date format dd/mm/yyyy .

It sorts the date and time at the same time and needs a date array of course to work.
In my case, I use a Hell Yes-Code version (from Mark’s version) where p8 parameter is the original (raw) Date Array:

p8.sort(function (a, b) {
  //  '13/9/2021 12:00:20'.split(' ') =>  ["13/9/2021", "12:00:20"]
  let DTa = a.split(' ');  DTb = b.split(' ');

  // '13/9/2021'.split('/') =>  ["13", "9", "2021"]
  let da = DTa[0].split('/');
  let db = DTb[0].split('/');

  let HMSa=DTa[1].split(':');
  let HMSb=DTb[1].split(':');

  //  Hour
  let ha = HMSa[0]; 
  let hb = HMSb[0];
 
  //  Minute
  let ma = HMSa[1]; 
  let mb = HMSb[1];

  //  Secs
  let sa = HMSa[2]; 
  let sb = HMSb[2];

  return     da[2] - db[2] || da[1] - db[1] ||  
             da[0] - db[0] || ha - hb       || 
              ma - mb      || sa - sb 
});
 
 // return the new sorted array
 return p8

My replit URL is:
https://Array-YesCode.gustavovalero.repl.co

Saludos!

1 Like