I am able to display the json array, but not the sorted version of it. It needs to be sorted so that the newer one comes first according to the date_posted field which is in the format of dd/mm/yyyy
[
{
"id": "5537a23050b2c722f390ab60",
"thumbImage": "http://lorempixel.com/175/115",
"title": "reprehenderit nisi occaecat magna eiusmod officia qui do est culpa",
"date_posted": "19/04/2020"
]
}
]
...
useEffect(() => {
fetch("http://localhost:3000/items")
.then((response) => response.json())
.then((json) => {
setAllItems(json);
console.log(json);
})
.catch((err) => {
console.log(`Error ${err}`);
});
}, []);
CodePudding user response:
allItems.sort((a, b) => {
a = a.date_posted.split('/');
b = b.date_posted.split('/');
return a[2] - b[2] || a[1] - b[1] || a[0] - b[0];
})
