How To Sort An Array Of Booleans In JavaScript

Sort an Array of Booleans in JavaScript

To sort an array of Booleans in JavaScript, we have tested effectively with two methods using the sort() method with nested ternary statements and using the sort() method with Number() method. Check out the details below to get more information.

Sort an array of Booleans in JavaScript

Using the sort() method with nested ternary statements

The best simple way is using the sort() method. The sort() sorts the elements of an array. It can sort alphabetically for strings (Ex: “American” comes before “London”). But sorting the numbers can procedure incorrect results. So you can provide a “compare function” if you want to use the sort() method.

Syntax:

array.sort(compareFunction)

Parameter: 

  • compareFunction: a function that defines a sort order. Depending on the arguments.

Function(a,b) {return a – b}

When sorting compares two values, it sends the values to the compare function and then sorts the value according to the returned value.

Return value: return value is an array with the items sorted.

Example:

In this example, we will use the sort() method with nested ternary statements. We create the function in the sort method with two variables, a assigned to true and b assigned to false. Then use nested ternary statements (a === b)? 0 : a? -1 : 1 to sort myArray. True value first, then false value. 

myArray = [false,  true, false, true, true, false, true, false, true, false];
console.log(myArray)
    
//using the sort() method with nested ternary statements  
myArray.sort(function(a, b) {

  // true values first then false value,assign a to true
  return (a === b)? 0 : a? -1 : 1;
 
});
    
console.log("After sorting myArray: ")
console.log(myArray);

Output

[false,  true, false, true, true, false, true, false, true, false]
After sorting myArray:
[true, true, true, true, true, false, false, false, false, false,]

Using the sort() method with Number() method

The other way to replace a comma with a dot is by using the split() method and join() method.

Number() method

Syntax:

Number(variable)

Parameter: 

  • variable: a variable with another type you want to convert a number.

Return value: return value is a number.

Example:

In this example, we will use the sort() method with Number to explicitly convert the boolean to a number. 

myArray = [false,  true, false, true, true, false, true, false, true, false];
console.log(myArray)
 
// using sort() method with Number to explicitly convert the boolean to a number
myArray.sort(function(a, b) {

   // assign a to true and b to false and using the number() method
   return Number(a) - Number(b);
});

console.log("After sorting myArray: ")
console.log(myArray);

Output

[false,  true, false, true, true, false, true, false, true, false]
After sorting myArray:
[true, true, true, true, true, false, false, false, false, false,]

Summary

In this tutorial, we have explained how to sort an array of Booleans in JavaScript by using two methods. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *