0

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) 
Christiaan
  • 3,609
  • 16
  • 19
  • Nothing in the question is related to react, so why the `reactjs` tag? – Andreas Oct 15 '20 at 09:13
  • Check the documentation of [`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). What does it return? And why is `a[0]`/`b[0]` wrong? – Andreas Oct 15 '20 at 09:15
  • 2
    Does this answer your question? [Sort JavaScript object by key](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Reyno Oct 15 '20 at 09:15

3 Answers3

1

You was on a correct way. You need to convert month to some numeric value and sort its.

Object.keys(currentMeetingTimes).sort( (a,b) => new Date(`${a} 1, 2020 12:00:00`).getMonth() - new Date(`${b} 1, 2020 12:00:00`).getMonth())

By the way:

Object.keys(currentMeetingTimes).sort( (a,b) => console.log(a[0])) // you need to use a
S
J
A
A
J
O
valentinmk
  • 322
  • 2
  • 9
1

const currentMeetingTimes = {
    May: 2464,
    September: 2265,
    June: 1656,
    April: 1595,
    August: 2111,
    July: 2005,
    October: 649,
}

function orderedByMonth(objNotSorted) {

    const objSortedMonths = {}

    const monthNames = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];

    for (const month of monthNames) {
        if (objNotSorted.hasOwnProperty(month)) {
            objSortedMonths[month] = objNotSorted[month]
        }
    }

    return objSortedMonths

}

console.log(orderedByMonth(currentMeetingTimes))
David Alvarez
  • 870
  • 6
  • 17
1

Here is how you order an object in a certain order (map object order), using key as relation and value as the order in the list, things to remember and take into consideration most javascript iteration functions already order your list primarily by number secondarily by alphabetic order(read more), best approach is to use map which takes insertion order into consideration, more in depth explanation can be read here.

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
};
const currentMeetingTimes = {
    May: 2464,
    September: 2265,
    June: 1656,
    April: 1595,
    August: 2111,
    July: 2005,
    October: 649
}
let ordered = {};
Object.keys(currentMeetingTimes).sort((a, b) => {
    return monthNames[a] - monthNames[b]
}).forEach(function(key) {
    ordered[key] = currentMeetingTimes[key];
});
//remeber to "loop" a object in correct order
//map is the most certain way other typ of loopes
//may brake the order
Object.entries(ordered).map((row) =>
    console.log({
       row
    })
)
Breezer
  • 9,844
  • 6
  • 27
  • 47