如何根据对象的布尔属性对数组进行排序

时间:2021-05-23 10:05:01

I'm trying to sort this array of objects by its boolean properties however, I'm struggling to find a solution with javascripts 'sort' method

我试图用它的布尔属性来对这个数组进行排序,但是我很难找到一个用java脚本排序的方法。

I'm trying to sort it, so the top item in the array would be 'offer = true', then 'shortlisted = true', and finally 'rejected = true'.

我正在对它进行排序,所以数组中的最上面的项是'offer = true',然后'shortlist = true',最后'reject = true'。

var toSort = [{
    offer: false,
    shortlisted: true,
    rejected: false,
    stage: 2
}, {
    offer: false,
    shortlisted: false,
    rejected: true,
    stage: null
}, {
    offer: true,
    shortlisted: true,
    rejected: false,
    stage: null
}, {
    offer: false,
    shortlisted: true,
    rejected: false,
    stage: 1
}];

This is the final result I would like to achieve

这是我想要达到的最终结果。

[{
    offer: true,
    shortlisted: true,
    rejected: false,
    stage: null
}, {
    offer: false,
    shortlisted: true,
    rejected: false,
    stage: 1
}, {
    offer: false,
    shortlisted: true,
    rejected: false,
    stage: 2
}, {
    offer: false,
    shortlisted: false,
    rejected: true,
    stage: null
}]

What is the best method to sort this array?

排序这个数组的最佳方法是什么?

3 个解决方案

#1


5  

You can use sort() like this.

可以像这样使用sort()。

var toSort = [{
  offer: false,
  shortlisted: true,
  rejected: false,
  stage: 2
}, {
  offer: false,
  shortlisted: false,
  rejected: true,
  stage: null
}, {
  offer: true,
  shortlisted: true,
  rejected: false,
  stage: null
}, {
  offer: false,
  shortlisted: true,
  rejected: false,
  stage: 1
}];

var result = toSort.sort(function(a, b) {
  return b.offer - a.offer ||
    b.shortlisted - a.shortlisted ||
    b.rejected - a.rejected
})

console.log(result)

#2


0  

One way is to assign a numerical score to each object, setting a higher score for values of higher precedence. For example:

一种方法是为每个对象分配数值分数,为优先级较高的值设置较高的分数。例如:

    var scoreObj = function(o) {
        var score = 0;
        if(o.offer) {
            score+=100;
        }
        if(o.shortlisted) {
            score+=10;
        }
        if(o.rejected) {
            score+=1;
        }
        return score;
    };
    var sorted = toSort.sort(function(a,b){
        return scoreObj(b) - scoreObj(a);
    });

#3


0  

A simple way :

一个简单的方法:

toSort.sort(function(a, b){
    var numberA = a.offer * 5 + a.shortlisted * 3 + a.rejected;
    var numberB = b.offer * 5 + b.shortlisted * 3 + b.rejected;
    return numberA < numberB
});

Why this works :

为何如此:

1) we can treat boolean as 0 or 1, so if a.offer is true we are adding 5 to numberA, if not 0

1)我们可以把布尔值看成0或1,如果a。出价是真的,我们在数字a上加5,如果不是0。

2) If offer property is true, even if all other are true, offer will still appear first (because 5 > 1 + 3 = 4)

2)如果offer property为true,即使其他所有的都为true, offer仍然会首先出现(因为5 > 1 + 3 = 4)

For more than 3 properties : This way we give a priority to the boolean properties you want first by giving it a numeric value that is greater than the sum of all the less priority properties

对于3个以上的属性:这样,我们可以为您想要的布尔属性赋予优先级,方法是给它一个数值,这个数值大于所有优先级较低的属性的总和

#1


5  

You can use sort() like this.

可以像这样使用sort()。

var toSort = [{
  offer: false,
  shortlisted: true,
  rejected: false,
  stage: 2
}, {
  offer: false,
  shortlisted: false,
  rejected: true,
  stage: null
}, {
  offer: true,
  shortlisted: true,
  rejected: false,
  stage: null
}, {
  offer: false,
  shortlisted: true,
  rejected: false,
  stage: 1
}];

var result = toSort.sort(function(a, b) {
  return b.offer - a.offer ||
    b.shortlisted - a.shortlisted ||
    b.rejected - a.rejected
})

console.log(result)

#2


0  

One way is to assign a numerical score to each object, setting a higher score for values of higher precedence. For example:

一种方法是为每个对象分配数值分数,为优先级较高的值设置较高的分数。例如:

    var scoreObj = function(o) {
        var score = 0;
        if(o.offer) {
            score+=100;
        }
        if(o.shortlisted) {
            score+=10;
        }
        if(o.rejected) {
            score+=1;
        }
        return score;
    };
    var sorted = toSort.sort(function(a,b){
        return scoreObj(b) - scoreObj(a);
    });

#3


0  

A simple way :

一个简单的方法:

toSort.sort(function(a, b){
    var numberA = a.offer * 5 + a.shortlisted * 3 + a.rejected;
    var numberB = b.offer * 5 + b.shortlisted * 3 + b.rejected;
    return numberA < numberB
});

Why this works :

为何如此:

1) we can treat boolean as 0 or 1, so if a.offer is true we are adding 5 to numberA, if not 0

1)我们可以把布尔值看成0或1,如果a。出价是真的,我们在数字a上加5,如果不是0。

2) If offer property is true, even if all other are true, offer will still appear first (because 5 > 1 + 3 = 4)

2)如果offer property为true,即使其他所有的都为true, offer仍然会首先出现(因为5 > 1 + 3 = 4)

For more than 3 properties : This way we give a priority to the boolean properties you want first by giving it a numeric value that is greater than the sum of all the less priority properties

对于3个以上的属性:这样,我们可以为您想要的布尔属性赋予优先级,方法是给它一个数值,这个数值大于所有优先级较低的属性的总和

相关文章