Js實現中文排序,並根據拼音首字母分組prototype
const pySegSort=(arr) =>{ if (!String.prototype.localeCompare) return null let letters = 'abcdefghjklmnopqrstwxyz'.split('') let zh = '阿八嚓噠妸發旮哈譏咔垃痳拏噢妑七呥扨它穵夕丫帀'.split('') let segs = [] letters.forEach((item, i) => { let cur = { letter: item, data: [] } arr.forEach((item) => { if(item.localeCompare(zh[i])>=0&&item.localeCompare(zh[i+1])<0){ cur.data.push(item) } }) if (cur.data.length) { cur.data.sort(function(a, b) { return a.localeCompare(b, 'zh') }) segs.push(cur) } }) return segs } console.log(pySegSort(['白案','百搭','白菜','百旺','王','李','張','劉','陳','楊','黃','吳','趙','周','的','徐','孫','馬','朱','胡','林','郭','何','高','羅','','鄭','梁','謝','宋','唐','許','鄧','馮','韓','曹','曾','彭','白大','蕭','蔡','F','潘','田','董','袁','於','餘','','葉','蔣','杜','蘇','魏','程','呂','丁','沈','任','白','卞']))