如何按对象属性的计算字段(如对象的2个属性的百分比)对对象数组进行排序,如下所示
myArray.sort( function(a,b){
var firstPer = calculatePercent(a[factor],a.Total_Records);
var secPer = calculatePercent(b[factor],b.Total_Records);
if(dir ==='asc')
return firstPer < secPer ? something : something
} )
最佳答案
您只需要返回一个数字:
myArray.sort(function(a,b) {
var firstPer = calculatePercent(a[factor], a.Total_Records);
var secPer = calculatePercent(b[factor], b.Total_Records);
if (dir === 'asc') {
return firstPer - secPer;
}
return secPer - firstPer;
}