javascript 排序而忽略文章('the','a'等)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 排序而忽略文章('the','a'等)相关的知识,希望对你有一定的参考价值。

const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog'];

const strip = band => band.replace(/^(a |the |an )/i, '').trim();
const sortedBands = bands.sort( (a, b) => strip(a) > strip(b) ? 1 : -1 );

document.querySelector('#bands').innerHTML = 
sortedBands
  .map(band => `<li>${band}</li>`)
  .join('');

以上是关于javascript 排序而忽略文章('the','a'等)的主要内容,如果未能解决你的问题,请参考以下文章