element, I am currently utilizing the
sort() method to arrange an Array based on date values. The issue arises when some elements within the data array are missing valid dates or have incorrect formats, leading to an error message stating: "Cannot read property 'getTime' of undefined."
An update to my situation is that I now have moment.js available for use. With this in mind, where would be the appropriate place to validate whether a date is valid before using it to sort the array?
Provided below is the sample data I am working with:
[{
"date": "2019-06-15 14:57:13",
"user": "john"
},
{
"date": "2019-06-15 05:48:01",
"user": "mike"
},
{
"date": "bad-date-format",
"user": "donna"
},
{
"date": "2019-06-08 10:45:09",
"user": "Ismil"
},
{
"date": "",
"user": "Daniel17"
}
]
In contrast, here is the desired output after sorting:
[
{
"date": "2019-06-15 14:57:13",
"user": "john"
},
{
"date": "2019-06-15 05:48:01",
"user": "mike"
},
{
"date": "2019-06-08 10:45:09",
"user": "Ismil"
},
{
"date": "bad-date-format",
"user": "donna"
},
{
"date": "",
"user": "Daniel17"
}
]