Skip to content
Related Articles

Related Articles

Sort an array of objects using Boolean property in JavaScript
  • Last Updated : 30 Jan, 2020

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.




<!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>

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.




<!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>

Output:

  • Before clicking on the button:

  • After clicking on the button:
My Personal Notes arrow_drop_up
Recommended Articles
Page :