js中文汉字按拼音排序

 1,datatable拓展 按中文排序

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "chinese-string-asc" : function (s1, s2) {
        return s1.localeCompare(s2,"zh");
    },
 
    "chinese-string-desc" : function (s1, s2) {
        return s2.localeCompare(s1,"zh");
    },
    "date-euro-pre": function ( a ) {
        var date = new Date(a);
        // 有三种方式获取
        var x = date.getTime();

        return x;
    },

    "date-euro-asc": function ( a, b ) {
        return a - b;
    },

    "date-euro-desc": function ( a, b ) {
        return b - a;
    }
} );

使用:[0]指得是数据列的第一列

"aoColumnDefs": [ { "type": "chinese-string","aTargets": [ 0 ] }],

 

2拼音排序:

var resultArray = array.sort(
    function compareFunction(param1, param2) {
        return param1.localeCompare(param2,"zh");
    }
);

3 拼音排序并按字母分类:


function pySegSort(arr,empty) {
    if(!String.prototype.localeCompare)
        return null;
     
    var letters = "*abcdefghjklmnopqrstwxyz".split('');
    var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('');
     
    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(empty || curr.data.length) {
            segs.push(curr);
            curr.data.sort(function(a,b){
                return a.localeCompare(b,"zh");
            });
        }
    });
    return segs;
}

​

原文地址:https://www.cnblogs.com/wteng/p/5658972.html

参考地址:https://datatables.net/plug-ins/sorting/#how_to_data_source

你可能感兴趣的:(项目遇到问题,java)