Sort an array of objects using Boolean property in JavaScript

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. Here 2 approaches are discussed here.
Approach 1:

  • Use JavaScript Array.sort() method.
  • In 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 1: This example implements the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Sort an array of objects by a
      Boolean property in JavaScript.
    </title>
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP">
    </p>
    <button onclick="GFG_Fun();">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color: green;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        a = [false, true, false, true, false];
        up.innerHTML =
            "Click on the button to sort the array"+
          "on boolean property.<br>Array = [" + a + "]";
  
        function GFG_Fun() {
            a.sort(function(x, y) {
                return (x === y) ? 0 : x ? -1 : 1;
            });
            down.innerHTML = "Sorted Array - [" + a + "]";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:

  • After clicking on the button:

Approach 2:



  • Use 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 2: This example implements the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        Sort an array of objects by a 
      Boolean property in JavaScript.
    </title>
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP">
    </p>
    <button onclick="GFG_Fun();">
        click here
    </button>
    <p id="GFG_DOWN"
       style="color: green;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        a = [false, true, false, true, false];
        up.innerHTML = 
          "Click on the button to sort the array on boolean "+
          "property.<br>Array = [" + a + "]";
  
        function GFG_Fun() {
            a.sort((a, b) => b - a).reverse();
            down.innerHTML = "Sorted Array - [" + a + "]";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:

  • After clicking on the button:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.