I am using electron, and I am trying to sort an array of objects relating to emails. I am trying to sort the array of objects based on dates, and want the option to sort either ascending or descending.
var emailObject = { file : filepath, EmailDate : parsedEmail.date, subject : parsedEmail.subject, from : parsedEmail.from, to: parsedEmail.to };
emailObjArray.push(emailObject);
I iterate through a number of emails, create the emailObject
and push that it an array emailObjArray
.
I tried using the following standard sort, but it has no effect.
emailObjArray.sort(function(a,b) {
return new Date(a.EmailDate).getTime() - new Date(b.EmailDate).getTime()
});
The EmailDate keys within the objects are as follows:-
Tue Jan 16 2018 16:40:03 GMT+0000 (Greenwich Mean Time)
Mon Nov 30 2020 16:47:28 GMT+0000 (Greenwich Mean Time)
Wed Feb 03 2021 18:18:44 GMT+0000 (Greenwich Mean Time)
Fri Jan 24 2020 12:36:17 GMT+0000 (Greenwich Mean Time)
Wed Jun 26 2019 17:53:03 GMT+0100 (British Summer Time)
When the sort is applied, the array remains in the same order.
I had reference to this answer https://stackoverflow.com/a/26759127 and tried to use this sortBy method as well, but again it had no effect.
Any clue where I am going wrong?