var list = [];
for(var i=0;i<100;i++){
list.push({x:parseInt(Math.random()*5),y:parseInt(Math.random()*5),z:parseInt(Math.random()*5)})
}
console.info(list)
mdSort(list,["x","y","z"])
console.info(list)
/*
list 待排序集合,例如 [{x:1,y:2,z:3},{...}]
plist 排序的属性名,例如 ["x","y","z"]
*/
function mdSort(list,plist){
if(list==null||list.length==0||plist==null||plist.length==0){
return;
}
sort(list,0,list.length-1,plist[0]);
for(var i=1;i<plist.length;i++){
sort2(list,plist[i-1],plist[i])
}
function sort2(list,p1,p2){
var start=0;
for(var i=1;i<list.length;i++){
if(list[i][p1]!=list[i-1][p1]){
sort(list,start,i-1,p2)
start = i;
}
if(i==list.length-1){
sort(list,start,i,p2);
}
}
}
function sort(list,start,end,p){
if(end-start<1){
return;
}
var L=start;
var R=end;
var temp=list[start];
while(L!=R){
while(R>L){
if(list[R][p]<temp[p]){
list[L]=list[R]
break;
}
R--;
}
while(L<R){
if(list[L][p]>temp[p]){
list[R]=list[L]
break;
}
L++;
}
}
list[R]=temp;
sort(list,start,R-1,p);
sort(list,R+1,end,p);
}
}