赞
踩
直接上代码:
compare(property){
return function(a,b){
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
传入需要排序的属性即可:
this.compare('number')
let arr = [ {name:'a',age:23}, {name:'b',age:33}, {name:'c',age:9}, {name:'d',age:17}, {name:'e',age:25}, {name:'f',age:18}, ] console.log(arr); let arr2 = arr.sort(function(a,b){ return a.age-b.age }) console.log(arr2);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。