I have displayed data of an API in a table form and I am trying to sort the data date wise but I am not getting it correct. If anyone can help me with it, it will be helpful.
const groupedByDate = res.data.reduce((meals, meal) => {
if (meal.date in meals) {
meals[meal.date].push(meal);
} else {
meals[meal.date] = [meal];
}
return meals;
}, {});
const sortedOnDate = Object.values(groupedByDate).sort((arr1, arr2) => {
if (arr1[0].date < arr2[0].date) {
return -1;
} else if (arr1[0].date > arr2[0].date) {
return 1;
}
return 0;
});
setData(sortedOnDate);
});
I have also added a code sandbox of what I have tried
It will be very helpful if u can edit the sandbox and provide a working example