拼音排序:
var arr = [{ a: 1, b: "我" }, { a: 1, b: "是" }, { a: 1, b: "帅" }, { a: 1, b: "哥" }, { a: 1, b: "你" }, { a: 1, b: "服" }, { a: 1, b: "不" }]; arr.sort( function compareFunction(param1, param2) { return param1.b.localeCompare(param2.b,"zh"); } ); // 结果为:[{a: 1, b: "不"},{a: 1, b: "服"},{a: 1, b: "哥"},{a: 1, b: "你"},{a: 1, b: "是"},{a: 1, b: "帅"},{a: 1, b: "我"}]
拼音排序并按字母分类:
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; } 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工具库