js,jq多维数组,根据指定键名排序,可降序,可升序
var obj = [{'pv':100,'uv':105},{'pv':123,'uv':132},{'pv':128,'uv':138},{'pv':88,'uv':68}];
console.log(sortByKey(obj,'pv','desc'));//
console.log(sortByKey(obj,'uv','asc'));
// 传入数组重新排序,排序功能(arr多维数组,key多维数组键名,order升序或降序)
function sortByKey(arr,key,order){
for(i=0;i<arr.length;i++){
for(j=i+1;j<arr.length;j++){
if(order=='desc'){
if(parseFloat(arr[i][key])<=parseFloat(arr[j][key])){
var min=arr[i];
arr[i]=arr[j];
arr[j]=min;
}
}else{
if(parseFloat(arr[i][key])>=parseFloat(arr[j][key])){
var max=arr[i];
arr[i]=arr[j];
arr[j]=max;
}
}
}
}
return arr;
}