你的位置:首页 > Java教程

[Java教程]js之封装sort实现json格式数据的排序


eg  :

data:{[

{"name":"paul","ege":12},

{"name":"paul","ege":13},

{"name":"paul","ege":14}

]};

 

var app={};

app.sortOn= function(arr,prop,sortCompareFunction){ 
if(sortCompareFunction && typeof sortCompareFunction ==="function"){
return arr.sort(sortCompareFunction);
}else{
var dup = Array.prototype.slice.call(arr, 0);
if (!arguments.length) return dup.sort();
return dup.sort(function(a,b){
var A= a[prop],
nA= isNaN(A),
B=b(prop),
nB=isNaN(B);
// 两者皆非number
if(nA && nB){
if (A==="") return -1;
if (B==="") return 1;
return (A===B?0:A>B?1:-1);
}
else if(nA) return -1;
else if(nB) return 1;
else return A===B?0:A>B?1:-1;
})
}
};