前台排序(对英文、汉字,数字都可以)

排序函数,json:排序对象,base:按照json中哪个字段排序,turn:正序、倒序*/
function sort(json,base,turn){
json.sort(function(n,m){
var s= n[base];
var e= m[base];
if(typeof(s)=='string'&&typeof(e)=='string'){
if(turn=='z'){
return s.localeCompare(e);
}else if(turn=='d'){
return e.localeCompare(s);
}
}else if(typeof(s)=='number'&&typeof(e)=='number'){
if(turn=='z'){
return s-e;
}else if(turn=='d'){
return e-s;
}
}
})
return json;
}
原文地址:https://www.cnblogs.com/learnings/p/6633572.html