I have an object that contains months with some number and I want to sort that in chronological order of months. I have the following data:
currentMeetingTimes= {
May: 2464,
September: 2265,
June: 1656,
April: 1595,
August: 2111,
July: 2005,
October: 649
}
I want this like:
currentMeetingTimes= {
April: 1595,
May: 2464,
June: 1656,
July: 2005,
August: 2111,
September: 2265,
October: 649
}
I've tried by using Object#keys
but not getting the correct result.
const monthNames = {
"January": 1,
"February": 2,
"March": 3,
"April": 4,
"May": 5,
"June": 6,
"July": 7,
"August": 8,
"September": 9,
"October": 10,
"November": 11,
"December": 12
};
currentMeetingTimes = Object.keys(currentMeetingTimes).sort(function(a, b) {
// sort based on the value in the monthNames object
return monthNames[a[0]] - monthNames[b[0]];
});
console.log('currentMeetingTimes:', currentMeetingTimes)