This question is an extention of this question here. Underscore js group by each month on each year in single level
The code which i am using is here.
const months = [
'January','February','March','April','May','June','July','August','September','October','November','December'
]
const result = flashMessage.reduce((res:any, item:any) => {
const date = new Date(item.created_at)
const year = date.getFullYear();
const month = months[date.getMonth()];
const existingYear=res[`${month} ${year}`] ||[];
const updated = [...(existingYear[month] || []), item]
const returnItem = {
title:item.title,
user_message:item.user_message,
created_at:item.created_at
};
res[`${month} ${year}`] = [...existingYear,returnItem]
return res
}, {});
The above code is producing expected result but it is in out of order here is the result of the above code. [![enter image description here][1]][1]
I want the result is in order. The order which i wanted is shown below.
March 2022
February 2022
January 2022
March 2021
February 2021
January 2021
So on how can i do that? [1]: https://i.stack.imgur.com/BS9ZS.png