In JavaScript, we often use the sort method to sort the array for a property with a Boolean value. We can also sort an array of objects by boolean property in JavaScript.
Sort an srray of objects by boolean property in JavaScript
To continue with this article, I will introduce you to the sort() method, which is used to sort the elements of the array in the order we want.
Syntax:
array.sort(compareFunction)
Parameter:
- compareFunction: This function helps us determine the order of elements in an array. According to the arguments we pass, this function must return a negative, 0, or positive number. The sort order is (negative, 0, and positive).
Example:
var nums = [3, 5, 8, 2, 1, 4];
// Sort array nums
nums.sort(function(a, b) {
return a - b
});
console.log(nums);
Output:
(6) [1, 2, 3, 4, 5, 8]
The above example shows that the original function has been sorted in ascending order.
After learning the sort() method, we see that to sort an array. We have to determine the compareFunction of the problem. Below are the two ways I built compareFunction to implement sort an array of objects by boolean property.
Use conditional statements
We can use the if conditional statement to check and give the appropriate return value to fulfill the request in this article.
Syntax:
if (condition) {
// Code that executes if condition is true
}
To sort an array of objects by the boolean property, I will build it with the conditional statement as follows:
var arr = [{
isActive: true
}, {
isActive: false
}, {
isActive: true
}, {
isActive: false
}, {
isActive: false
}, {
isActive: true
}];
// Sort array arr
arr.sort(function(x, y) {
// True values first
if (x.isActive === y.isActive) {
return 0;
} else {
if (x.isActive)
return -1;
else
return 1;
}
});
console.log(arr);
Output:
[
{ isActive: true },
{ isActive: true },
{ isActive: true },
{ isActive: false },
{ isActive: false },
{ isActive: false }
]
I was outputting the results before and after sorting in the above code. I sort the array in order of actual before and false after. If I wanted to sort by false, I did it in the comment code in the above example.
Use toLocaleDateString() method
(-) property
// Sort array arr
arr.sort(function(a, b) {
// True values first
return a.isActive - b.isActive
});
console.log(arr);
Next is the Conditional Operator:
arr.sort(function(a, b) {
// True values first
return (x === y) ? 0 : x ? -1 : 1;
});
console.log(arr);
Finally, the combination of (<), (*), (-) operator:
arr.sort(function(a, b) {
// True values firstreturn
(a.isActive < b.isActive) * 2 - 1
// False values first
return (a.isActive > b.isActive) * 2 - 1
});
console.log(arr);
All the above ways have the same output:
[
{ isActive: false },
{ isActive: false },
{ isActive: false },
{ isActive: true },
{ isActive: true },
{ isActive: true }
]
Summary
I showed you two ways to sort an array of objects by boolean property in JavaScript. Try following me. It will help you remember the above ways and understand them better. I hope this article helps you and your program. Good luck.
Maybe you are interested:
- Sort an Array with NULL values coming last in JavaScript
- Sort an Array of strings ignoring the Case in JavaScript
- Sort an Array of Booleans in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css