DEV Community

Cover image for How to sort an array of month names -- Javascript
NasreenKhalid
NasreenKhalid

Posted on

4

How to sort an array of month names -- Javascript

Hi there, so I was working with some dates and months arrays in javascript and came across this fact that if you sort an array of month names in javascript using the sort() method, it will sort the array in alphabetical order making 'April' the first month of the year which is not what we want. Following is the output when you simply use sort() method on an array of names in Javscript:



const months = ['January', 'March', 'February', 'May', 'June','April'];

months.sort()
console.log(months);


Enter fullscreen mode Exit fullscreen mode

Below is the output:

Image description

So, what we should do is to define a new array which contains the names of the months in chronological order and then compare both the arrays and also we need to define a comparison function which will compare the indices of both the arrays using the indexOf method, the function will look like this:



const months = ['January', 'March', 'February', 'May', 'June','April'];

months.sort((a, b) => {
  const monthOrder = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  return monthOrder.indexOf(a) - monthOrder.indexOf(b);
});

console.log(months);


Enter fullscreen mode Exit fullscreen mode

The resulting array of 'months' will show the names in the correct order as we want:

Image description

Hope you find this article useful while brushing up your basic javascript skills.
Have a good time coding...

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (2)

Collapse
 
ant_f_dev profile image
Anthony Fung

Interesting approach - thanks for sharing!

One potential disadvantage is that indexOf has to be called multiple times. One way around that might be to declare the order in an object, e.g.

const months = ['January', 'March', 'February', 'May', 'June','April'];

const monthOrder = {
  January: 0,
  February: 1,
  March: 2,
  April: 3,
  May: 4,
  June: 5,
  July: 6,
  August: 7,
  September: 8,
  October: 9,
  November: 10,
  December: 11
};

months.sort((a, b) => {  
  return monthOrder[a] - monthOrder[b];
});
Enter fullscreen mode Exit fullscreen mode

That said, the performance impact for small lists like this is probably negligible.

Collapse
 
nasreenkhalid profile image
NasreenKhalid

Thank you for sharing this :)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →