js实现通讯录好友排序

    //中文转拼音
      chineseToEnglish = function(c){
        var idx = -1;
        var MAP = 'ABCDEFGHJKLMNOPQRSTWXYZ';
        var boundaryChar = '驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳';
        if (!String.prototype.localeCompare) {
          throw Error('String.prototype.localeCompare not supported.');
        }
        if (/[^\u4e00-\u9fa5]/.test(c)) {
          return c;
        }
        for (var i = 0; i < boundaryChar.length; i++) {
          if (boundaryChar[i].localeCompare(c, 'zh-CN-u-co-pinyin') >= 0) {
            idx = i;
            break;
          }
        }
        return MAP[idx];
    }

        //获取第一个字母
        getFirstUpperChar = function(str){
        var string = String(str);
        var c = string[0];
        if (/[^\u4e00-\u9fa5]/.test(c)) {
          return c.toUpperCase();
        }
        else {
          return this.chineseToEnglish(c);
        }
      }

        //排序
        sortFriend(list) {
                var c = 'A'.charCodeAt();
        for(; c <= 'Z'.charCodeAt(); c++ ){
            map[String.fromCharCode(c)] = [];
        }
        map['#'] = [];
        var firstCharUpper;
                list.forEach(function(item){
            firstCharUpper = getFirstUpperChar(item);
            if (map.hasOwnProperty(firstCharUpper)) {
            map[firstCharUpper].push(item);
            } else {
            map['#'].push(item);
            }
        });
                return map;
        }

你可能感兴趣的:(js实现通讯录好友排序)