I am trying to sort data based on descending order of publishDate but it's not working. In some array, publishDate is coming and in some array, it's not coming.
[
{
"id": "brexit-delay",
"title": "Brexit Delay",
"publish": {
"publishDate": "2019-8-30T12:25:47.938Z",
}
},
{
"id": "brexit-delay",
"title": "Example 3"
},
{
"id": "brexit-delay",
"title": "Example 2",
"publish": {
"publishDate": "2019-6-30T12:25:47.938Z",
}
},
{
"id": "brexit-delay",
"title": "Example 5"
},
{
"id": "brexit-delay",
"title": "Example 5",
"publish": {
"publishDate": "2019-10-25T12:25:47.938Z",
}
}
]
Js code:
data.sort(function(a, b){
if("publish" in a && "publish" in b){
return new Date(a.publish.publishDate) - new Date(b.publish.publishDate)
}
} );
publishDateis missing? Are they later that everything or sooner?'publish' in awhile the key is calledpublishDatein the original object, so I guess it should be'publishDate' in apublishDateis a key ofpublish, so it doesn't matter. I'm assuming that ifpublishkey exist thenpublish.publishDatewill also have some date value.publishDatetreat as soonerpublishkey ;)