[Javascript] Case insensitive sorting for string arrays

We look at the default Array.prototype.sort behavior and discuss how you can do case insensitive string sorting.

 

const foo = [
  'Alpha',
  'beta',
  'Gamma',
  'delta'
];
foo.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
foo.forEach(x => console.log(x));

/*
"Alpha"
"beta"
"delta"
"Gamma"
*/

 

posted @ 2017-04-19 01:10  Zhentiw  阅读(198)  评论(0编辑  收藏  举报