• js中文汉字按拼音排序


    代码1,拼音排序:

    var array = ['武汉', '北京', '上海', '天津'];
    var resultArray = array.sort(
        function compareFunction(param1, param2) {
            return param1.localeCompare(param2,"zh");
        }
    );
    console.log(resultArray);

    火狐浏览器 resultArray 结果为:[ '北京' , '上海' , '天津' ,'武汉' ] ;

    代码2,拼音排序并按字母分类:

     1 function pySegSort(arr,empty) {
     2     if(!String.prototype.localeCompare)
     3         return null;
     4      
     5     var letters = "*abcdefghjklmnopqrstwxyz".split('');
     6     var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('');
     7      
     8     var segs = [];
     9     var curr;
    10     $.each(letters, function(i){
    11         curr = {letter: this, data:[]};
    12         $.each(arr, function() {
    13             if((!zh[i-1] || zh[i-1].localeCompare(this,"zh") <= 0) && this.localeCompare(zh[i],"zh") == -1) {
    14                 curr.data.push(this);
    15             }
    16         });
    17         if(empty || curr.data.length) {
    18             segs.push(curr);
    19             curr.data.sort(function(a,b){
    20                 return a.localeCompare(b,"zh");
    21             });
    22         }
    23     });
    24     return segs;
    25 }
    JSON.stringify(pySegSort(["我","不","懂","爱","啊","按","已","呀","选","县"]))
    //结果
    "[
      {"letter":"a","data":["","",""]},
      {"letter":"b","data":[""]},
      {"letter":"d","data":[""]},
      {"letter":"w","data":[""]},
      {"letter":"x","data":["",""]},
      {"letter":"y","data":["",""]}
    ]"
     
     
    扩展:http://blog.csdn.net/testcs_dn/article/details/25116655         ------JS获取中文拼音首字母,并通过拼音首字母快速查找页面内的中文内容
               http://www.jb51.net/article/100864.htm      ----------JS实现超简单的汉字转拼音功能示例
               https://github.com/sxei/pinyinjs                  -----------一个实现汉字与拼音互转的小巧web工具库
  • 相关阅读:
    隔离级别 && SNAPSHOT
    多态性&& 虚函数 && 抽象类
    socket编程
    [APIO2015]巴邻旁之桥
    LuoguP3701 「伪模板」主席树
    线段树标记永久化
    [HNOI2015]开店
    NOIP2017划水记
    FFTNTT总结
    [THUWC 2017]在美妙的数学王国中畅游
  • 原文地址:https://www.cnblogs.com/wteng/p/5658972.html
Copyright © 2020-2023  润新知