I have an array containing many objects that each have a date property in the following format: { date: 'December 2017' }
. I want to sort all of them in descending order. How can I achieve this?
I've tried using a secondary object tying each of the month names to its corresponding number and then converting each into a new Date to sort. I'm successfully converting each of the objects into the new Date format but I'm unsure how to then use this to sort my original array.
let dates = [{ date: 'December 2016' }, { date: 'December 2017' }, { date: 'November 2016' }, { date: 'August 2015' }, { date: 'June 2018' }, { date: 'May 2017' }];
let months = { "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12 };
let newDates = [];
for (var i = 0; i < dates.length; i++) {
newDates.push(new Date(dates[i].date.replace(' ', ', ')))
}
newDates.sort((a, b) => {
return b - a;
})