JavaScript30 - Day 17

By Camilla Krag Jensen |

Day 17 of #javascript30 is simply sorting an array of band names, but without considering articles like ’the’, ‘a’ or ‘an’.

I felt good about this one. I am starting to feel quite at home with JavaScript’s array functions. I solved it without watching the video first and with very little code:

const bands = [
	'Another Band Name', 
	'An Amalgam', 
	'The Frosty Shred', 
	'Buffalo Of Arrow', 
	'The Thrash Paranoia', 
	'During Temperature', 
	'A During Temperature', 
	'The Drug Meeting'
	];
const regex = new RegExp(/^(The|An|A)\ /, 'i');
bands.sort((a, b) => a.replace(regex, '') > b.replace(regex, '') ? 1 : -1);
document.querySelector('#bands').innerHTML = bands.map(band => `<li>${band}</li>`).join('');

Because sort() mutates the array, I needed very few lines of codes. I have linked to it before, but here is a list of array functions that mutate the array. It is important to understand, and nifty to know!