javascript 对中文按拼音排序,将指定字符列在第一位
var arr = ["广州","北京","徐州","山东","上海"];
document.write(arr.sort(function (a,b){
//alert("a:" +a+ "\nb:"+b + "\nres: " + a.localeCompare(b));
if(a.indexOf("上海") >= 0 ) {
return -1;
}else if(b.indexOf("上海") >= 0 ) {
return 1;
} else {
return a.localeCompare(b);
}
}))
//结果:上海,北京,广州,山东,徐州
document.write(arr.sort(function (a,b){
//alert("a:" +a+ "\nb:"+b + "\nres: " + a.localeCompare(b));
if(a.indexOf("上海") >= 0 ) {
return -1;
}else if(b.indexOf("上海") >= 0 ) {
return 1;
} else {
return a.localeCompare(b);
}
}))
//结果:上海,北京,广州,山东,徐州