中文名排序,利用中文名简拼筛选名字或其它中文单词
var names = ["王思聪", "张柏芝", "郭德纲", "林志颖", "潘长江", "马云", "冯小刚"];
function getShortPinyin(word) {
var idx = -1;
var MAP = 'ABCDEFGHJKLMNOPQRSTWXYZ';
var boundaryChar = '驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳';
if (!String.prototype.localeCompare) {
throw Error('String.prototype.localeCompare not supported.');
}
return _(word.split('')).map(function(c) {
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];
}).value().join('');
}
var names_with_pinyin = _(names).map(function(name) {
var name_with_pinyin = {
name: name,
pinyin: getShortPinyin(name)
}
return name_with_pinyin;
}).sortBy('pinyin').value();
/* Output
** 冯小刚(FXG)
** 郭德纲(GDG)
** 林志颖(LZY)
** 马云(MY)
** 潘长江(PZJ)
** 王思聪(WSC)
** 张柏芝(ZBZ)
*/
示例地址:https://jsfiddle.net/zhwrkfeh/2/