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?
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?
Share Improve this question edited Dec 12, 2016 at 16:11 James Moran asked Dec 12, 2016 at 16:05 James MoranJames Moran 6301 gold badge7 silver badges14 bronze badges 5- How are you sorting this by the Boolean properties? Can you be more specific? – Sara Fuerst Commented Dec 12, 2016 at 16:07
- What have you tried? How did it fail? How exactly do you want the array sorted? – user5734311 Commented Dec 12, 2016 at 16:07
- Which field is this being sorted by? – WillardSolutions 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'. – James Moran Commented Dec 12, 2016 at 16:12
- 2 Glad you accepted my answer but I believe @NenadVracar answer is the best one ;) – juvian Commented Dec 12, 2016 at 16:29
3 Answers
Reset to default 13You 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)
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);
});
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