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.

6
  • 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 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). Commented Apr 9, 2021 at 8:07
  • Check out my answer, maybe it will help you out :) you can make it in one line by just adding simple library Commented Apr 9, 2021 at 8:13
  • If you use an answer from the dupe target, 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... Commented Apr 9, 2021 at 8:23
  • your date string does not have a valid format, you need to change it you can use 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() Commented Apr 9, 2021 at 8:27
  • @NverAbgaryan "your date string does not have a valid format" - What is not valid on that valid date format? Commented Apr 9, 2021 at 8:28

3 Answers 3

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; }

Sign up to request clarification or add additional context in comments.

1 Comment

And you could first look for a dupe.
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>

1 Comment

Does this answer your question? Let me know if you need any help ^^! @Dwight Danger Schrute
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.