In this lesson I learned how to sort an array while stripping out specific phrases to ignore those phrases in the sort.
First I grab the array of 'bands'. I then sort the bands alphabetically using a ternary operator (bands.sort((a, b) => a > b ? 1 : -1)). In-order to strip out the phrases I run a function strip() on both 'a' and 'b'. ((a,b) => sort(a) > sort(b) ? 1 : -1).
In strip(bandName) - Return a .replace().trim() string that removes all of the set phrases using a regex and trims off extra spaces (.replace(/^(a |the |an)/i, '').trim()).
Finally set the .innerHTML of the <ul> to 'sortedBands' and output an <li> element for each item in the array with .map(). join() to convert it all into one string.
See the Pen
JavaScript30 - 17 - Sort Without Articles by Corey Noble (@CoreyNoble)
on CodePen.