参考:https://www.cnblogs.com/ttjm/p/10190074.html
原文内容
js">function compare(property,sort){
return function(a,b){
var value1=a[property];
var value2=b[property];
if(sort==1){
return value1 - value2;
}else{
return value2 - value1;
}
}
};
调用 arr.sort(compare(property,1))
sort =1 是正序
sort = -1 是倒序
个人使用
js">// 对数组根据某属性字段进行排序
compare(property, sortRule){
let that = this;
return function(obj1, obj2){
let value1 = obj1[property];
let value2 = obj2[property];
if(sortRule == 'ASC' || sortRule == 'asc'){
//升序
return value1 - value2;
} else if(sortRule == 'DESC' || sortRule == 'desc'){
//降序
return value2- value1;
} else {
that.msgError("排序规则有误");
}
}
},
// 对树根据项次排序
sortBySeq(list, sortRule){
if(list.length > 0){
// 对最高父节点排序
list.sort(this.compare('materialtypeSeq', sortRule));
// 递归排序子节点
list.forEach(item => {
if(item.children != undefined){
this.sortBySeq(item.children, sortRule);
}
});
}
return;
},