Js实现中文排序,并根据拼音首字母分组

Js实现中文排序,并根据拼音首字母分组

const pySegSort=(arr) =>{
    if (!String.prototype.localeCompare) return null
    let letters = 'abcdefghjklmnopqrstwxyz'.split('')
    let zh = '阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀'.split('')
    let segs = []
    letters.map((item, i) => {
        let cur = { letter: item, data: [] }
        arr.map((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','潘','田','董','袁','于','余','','叶','蒋','杜','苏','魏','程','吕','丁','沈','任','白','卞']))

你可能感兴趣的:(Js实现中文排序,并根据拼音首字母分组)