0

I have an array of objects so structured.

let array = [
    {date: "22/03/2021 17:57", y: 10, type: "dil"},
    {date: "22/03/2021 17:58", y: 1, type: "dil"},
    {date: "15/04/2021 14:52", y: 3, type: "dil"},
    {date: "24/03/2021 14:52", y: 4, type: "dil"},
    {date: "01/04/2021 14:52", y: -2, type: "spp"},
    {date: "24/03/2021 14:53", y: -5, type: "spp"},
    {date: "18/04/2021 16:28", y: 3, type: "spp}
]

I have to sort it by date but I can't figure out how to do it because date is a string and if I use the sort method

array.sort((a,b) => (a.x > b.x) ? 1 : ((b.x > a.x) ? -1 : 0))

it gets ordered based on the first two characters, and not properly by confronting year, month, date, hour, and minutes.

Any ideas? I'm sure it is something very easy but I'm quite stuck.

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • 1
    Reformat the date to ISO-8601 and use that in your comparison. Sadly while javascript does provide a `toISOString` method on dates, [it doesn't have a built-in format for european datetimes](https://stackoverflow.com/questions/10430321/how-to-parse-a-dd-mm-yyyy-or-dd-mm-yyyy-or-dd-mmm-yyyy-formatted-date-stri) only RFC 2822 and ISO8601, and doesn't provide generic format strings, so you will have to parse the inputs by hand (or with something like moment.js). – Masklinn Apr 09 '21 at 08:07
  • Check out my answer, maybe it will help you out :) you can make it in one line by just adding simple library – Sowam Apr 09 '21 at 08:13
  • If you use an answer from the [dupe target](https://stackoverflow.com/questions/43222209/unable-to-sort-date-time-in-javascript-with-format-dd-mm-yyyy-hhmm), then go with the one from RobG. Changing the prototype of `String` just for this one specific case, as in the accepted answer, is overkill... – Andreas Apr 09 '21 at 08:23
  • your date string does not have a valid format, you need to change it you can use https://date-fns.org/docs/Getting-Started this library can help you a lot to fix the date format. so after having a fixed date format you can just do soring like this new Date(date1).valueOf() - new Date(date2).valueOf() – Nver Abgaryan Apr 09 '21 at 08:27
  • @NverAbgaryan _"your date string does not have a valid format"_ - What is not valid on that valid date format? – Andreas Apr 09 '21 at 08:28
  • @Andreas So this new Date function will return an invalid Date error after running this code ` new Date('18/04/2021 16:28') `, you need to put the year first – Nver Abgaryan Apr 09 '21 at 08:47

3 Answers3

2

You could create an ISO 8601 date string and sort by string.

const
    getISO = string => string.replace(/(..)\/(..)\/(....) (..):(..)/, '$3-$2-$1 $4:$5'),
    array = [{ date: "22/03/2021 17:57", y: 10, type: "dil" }, { date: "22/03/2021 17:58", y: 1, type: "dil" }, { date: "15/04/2021 14:52", y: 3, type: "dil" }, { date: "24/03/2021 14:52", y: 4, type: "dil" }, { date: "01/04/2021 14:52", y: -2, type: "spp" }, { date: "24/03/2021 14:53", y: -5, type: "spp" }, { date: "18/04/2021 16:28", y: 3, type: "spp" }];

array.sort((a, b) => getISO(a.date).localeCompare(getISO(b.date)));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can use moment.Js with the formula parse Date looks like

const parseDate = strDate => moment(strDate, "DD/MM/YYYY HH:mm");

After that, you can sort the array by sort method like

array.sort((a, b) => parseDate(a.date) - parseDate(b.date));

Full code snippet

let array=[{date:"22/03/2021 17:57",y:10,type:"dil"},{date:"22/03/2021 17:58",y:1,type:"dil"},{date:"15/04/2021 14:52",y:3,type:"dil"},{date:"24/03/2021 14:52",y:4,type:"dil"},{date:"01/04/2021 14:52",y:-2,type:"spp"},{date:"24/03/2021 14:53",y:-5,type:"spp"},{date:"18/04/2021 16:28",y:3,type:"spp"}];

const parseDate = strDate => moment(strDate, "DD/MM/YYYY HH:mm");
array.sort((a, b) => parseDate(a.date) - parseDate(b.date));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
0

There are three ways you can do this first. If the dates are in DD/MM/YYYY, just convert them to YYYYMMDD

array.sort(function(date1,date2) {
  date1 = date2.split('/').reverse().join('');
  date1 = date2.split('/').reverse().join('');
  return date1 > date2 ? 1 : date1 < date2 ? -1 : 0;
});

In the second method you can use String.prototype.localeCompare()

array.sort(function(date1, date2) {
 return date1.localeCompare(date2);  
})

third is faily easy you can convert both into Date object and compare

thenaamsake
  • 264
  • 3
  • 12