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...

Image of Stellar post

πŸš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read 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 :)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

πŸ‘‹ Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay