7

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

I'm trying to sort it, so the top item in the array would be 'offer = true', then 'shortlisted = true', and finally 'rejected = 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?

5
  • How are you sorting this by the Boolean properties? Can you be more specific? Commented Dec 12, 2016 at 16:07
  • What have you tried? How did it fail? How exactly do you want the array sorted? Commented Dec 12, 2016 at 16:07
  • Which field is this being sorted by? Commented Dec 12, 2016 at 16:07
  • Sorry, I have updated the post. I'm trying to sort the array based on the boolean properties that are true/false. In this case it should sort each item as follows - 'offer = true', then 'shortlisted = true', and finally 'rejected = true'. Commented Dec 12, 2016 at 16:12
  • 2
    Glad you accepted my answer but I believe @NenadVracar answer is the best one ;) Commented Dec 12, 2016 at 16:29

3 Answers 3

13

You can use sort() like this.

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)

Sign up to request clarification or add additional context in comments.

Comments

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);
    });

Comments

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

2) If offer property is true, even if all other are true, offer will still appear first (because 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

2 Comments

So why not 4 and 2 instead of 5 and 3..? That would make some binary sense to me.. Nenad's answer seems better to me though.
@Redu indeed 4 and 2 would work as well, and I also believe the other answer is better ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.