Sort an array of objects using Boolean property in JavaScript Last Updated : 24 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given the JavaScript array containing Boolean values. The task is to sort the array on the basis of Boolean value with the help of JavaScript. There are two approaches that are discussed below: Table of Content Using Array.sort() Method and === OperatorUsing Array.sort() and reverse() MethodsUsing a Custom Comparator Function for ObjectsUsing Array.sort() Method and === OperatorUse JavaScript Array.sort() method.In the Comparison condition, Use === operator to compare the Boolean objects.Return 0, 1, and -1 means equal, greater, and smaller respectively depending upon the comparison.Example: This example implements the above approach. JavaScript let arr = [false, true, false, true, false]; function GFG_Fun() { arr.sort(function (x, y) { return (x === y) ? 0 : x ? -1 : 1; }); console.log("Sorted Array - [" + arr + "]"); } GFG_Fun(); OutputSorted Array - [true,true,false,false,false] Using Array.sort() and reverse() MethodsUse JavaScript Array.sort() method.In Comparison condition, Subtract the first element from the second one to compare the objects and return that value.Use .reverse() method, If the result is needed to be reversed.Example: This example implements the above approach. JavaScript let arr = [false, true, false, true, false]; function GFG_Fun() { arr.sort((a, b) => b - a).reverse(); console.log("Sorted Array - [" + arr + "]"); } GFG_Fun(); OutputSorted Array - [false,false,false,true,true] Using a Custom Comparator Function for ObjectsWhen dealing with objects that have a Boolean property, a custom comparator function can be used to sort the array based on the Boolean property. Example: This example demonstrates sorting an array of objects based on a Boolean property. JavaScript let arr = [ { name: "Alice", active: false }, { name: "Bob", active: true }, { name: "Charlie", active: false }, { name: "David", active: true }, { name: "Eve", active: false } ]; function sortObjectsByBoolean(arr) { arr.sort((a, b) => b.active - a.active); } sortObjectsByBoolean(arr); console.log(arr); Output[ { name: 'Bob', active: true }, { name: 'David', active: true }, { name: 'Alice', active: false }, { name: 'Charlie', active: false }, { name: 'Eve', active: false } ] Comment More infoAdvertise with us Next Article How to sort an array of objects by property values ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Sort Array of Objects By String Property Value in JavaScript Sorting arrays of objects based on a string property can be helpful for handling user data or dynamic lists. Here are different ways to sort an array of Objects By String Property Value.1. Using localeCompare() Method â Most UsedThe JavaScript localeCompare() method returns a number indicating wheth 3 min read How to Find Property Values in an Array of Object using if/else Condition in JavaScript ? Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find 5 min read How to Sort an Array of Objects Based on a Key in JavaScript ? In JavaScript, sorting an array of objects based on the key consists of iterating over the array, applying the sort() method or other approaches, and arranging the array in the desired sorting order. Table of Content Using sort() methodUsing Custom Sorting FunctionUsing Lodash _.orderBy() MethodUsin 3 min read How to sort an array of object by two fields in JavaScript ? We have given an array of objects and the task is to sort the array of elements by 2 fields of the object. There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same 3 min read How to sort an array of objects by property values ? In this article, we will try to understand how to sort an array of objects by property values in JavaScript with the help of certain examples. Pre-requisite: Array of Objects in JavaScript Example: Input:[ { name: "Ram", age: 17 }, { name: "Mohan", age: 30 }, { name: "Shyam", age: 15 }, { name: "Shy 4 min read JavaScript - Sort an Array in Reverse Order in JS JS array.sort() Method sorts the array in ascending order. We can make it descending or reverse order using array.reverse() method. Below are the following ways to sort an array in reverse order:1. Using array.sort() with array.reverse()First, sort the given array of strings using JS array.sort() me 2 min read Like