UPDATE: Finally cracked the code on this issue. I initially assumed that Date was interpreting the date ("29-04-2020")
as DD-MM-YYYY
, when it should actually be MM-DD-YYYY
.
For instance, here's an object array I'm working with:
let todos: object[] = [
{
'id': 1,
'title': 'Test',
'description': 'Just a new task',
'priority': 1,
'status': 'during',
'date': new Date('29-05-2020')
},
{
'id': 2,
'title': 'test23',
'description': 'Just a new task23',
'priority': 3,
'status': 'done',
'date': new Date('29-05-2020')
},
{
'id': 3,
'title': 'test3',
'description': 'Just a new task3',
'priority': 2,
'status': 'in process',
'date': new Date('29-05-2020')
},
{
'id': 4,
'title': 'test3',
'description': 'Just a new task3',
'priority': 1,
'status': 'during',
'date': new Date('29-05-2020')
},
{
'id': 5,
'title': 'test3',
'description': 'Just a new task3',
'priority': 2,
'status': 'done',
'date': null
},
{
'id': 6,
'title': 'test3',
'description': 'Just a new task3',
'priority': 1,
'status': 'in process',
'date': new Date('29-05-2020')
},
]
My goal is to sort these objects by date while excluding keys with a null value (hence the use of the if
operator). To achieve this, I've created a function:
function sortByDate() {
todos.sort((a, b) => {
console.log(a, b)
if (a['date'] == null) {
return 1;
}
if (b['date'] == null) {
return -1;
}
return a['date'].getTime() - b['date'].getTime()
})
return todos
}
However, upon sorting, I noticed that where 'date' property existed but wasn't null, instead of correct date values, I received "Invalid date" entries in the 'date' key. Now, I'm stuck and unsure how to rectify this.