Hope you can help me with this question I have, that I'm pretty sure it's simple but I feel I'm missing some basic concepts here.
I have an array of objects like
[{
"id":"123",
"creationUser":"user1",
"updateUser":null,
"creationDate":1517495569000,
"updateDate":null,
"text":"Hello World"
},
{
"id":"543",
"creationUser":"user2",
"updateUser":"user3",
"creationDate":1517912985769,
"updateDate":1517921704448,
"text":"Hello people"
},
{
"id":"847",
"creationUser":"user 4",
"updateUser":null,
"creationDate":null,
"updateDate":1517913015110,
"text":"Text 1"
},
{
"id":"344",
"creationUser":"user 1",
"updateUser":"central",
"creationDate":1517912979283,
"updateDate":1517923926834,
"text":"Aloha!"
}]
As you can see there are some objects that doesn't have been updated so those values are set to null, but others have been updated, so what I would like to do is to order that array by creation date unless it has been updated, which mean that the updatedDate is the key value to compare this array.
I have tried:
let comments = conversation.sort(
(a,b) => {
if (a.updateDate){
return (a.creationDate - b.updateDate);
} else {
return (b.creationDate - a.updateDate);
}
});
But obviously it only works when comparing non updated objects. I'm pretty sure I'm missing something but I'm not sure, I also thought on splitting the array into updated array and non updated array and then merege it, but it sounds a bit hacky to me.
Please please, if you can give me a hint on this, it would be great!
Thanks a lot!
conversation.sort((a, b) => {is missing the=>.