将数组按照拼音排序,并按字母排序

按拼音排序:

var resultArray = arr.sort(          //数组按照abcd排列
            function compareFunction(param1, param2) {
                return param1.localeCompare(param2,"zh");
            }
    );

将数组按照拼音排序(多音字无法辨别,需手动区分),并按字母分类:

function pySegSort(arr,empty) {
        if(!String.prototype.localeCompare)
            return null;

        var letters = "*ABCDEFGHJKLMNOPQRSTWXYZ".split('');
        var zh = "阿吧擦大额发噶哈鸡看啦嘛拿哦怕去让仨他哇西呀咋".split('');
        console.log(letters.length+"-----------------"+zh.length)
        var segs = [];
        var curr;
        $.each(letters, function(i){
            curr = {letter: this, data:[]};
            $.each(arr, function() {
//                if((!zh[i-1] || zh[i-1].localeCompare(this,"zh") <= 0) && this.localeCompare(zh[i],"zh") == -1) {
//                    curr.data.push(this);
//                }
                if((!zh[i-1] || zh[i-1].localeCompare(this,"zh") <= 0) && this.localeCompare(zh[i],"zh") == -1) {
                    curr.data.push(this);
                }
            });
            if(empty || curr.data.length) {
                segs.push(curr);
                curr.data.sort(function(a,b){
                    return a.localeCompare(b,"zh");
                });
            }
        });
       // console.log("1--------------"+typeof segs );
        return segs;
    }

你可能感兴趣的:(将数组按照拼音排序,并按字母排序)