/**
* @param {boolean} bool: 为true为升序,false为降序
* @param {string} attribute: 属性名称 不传时直接比较obj1,obj2
*/
function compare(attribute,bool = true){
return function(obj1,obj2){
let val1 = attribute ? obj1[attribute] : obj1
let val2 = attribute ? obj2[attribute] : obj2
if(val1 > val2){
return bool ? 1 : -1
}else if( val1 < val2){
return bool ? -1 : 1
}
return 0
}
}
sort排序问题
回调函数(a,b) : a,b比较小于0,a<b, a排在b前面;比较大于0,a>b, b排在a前面