js排序

(1)
//数组对象方法排序:
function sortByKey(array,key){
return array.sort(function(a,b){
var x=a[key];
var y=b[key];
return ((x<y)?-1:((x>y)?1:0));
});
}
(2)
console.log(arr.sort(compare('shuxingmingysx')))
compare(pro) {
console.log(pro);
return function(obj1, obj2) {
var val1 = obj1[pro];
var val2 = obj2[pro];
if (val1 < val2) {
//正序
return 1;
} else if (val1 > val2) {
return -1;
} else {
return 0;
}
};
},

posted @ 2019-05-23 19:38  A影子放風箏  阅读(125)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css