Open In App

jQuery | uniqueSort() method

Improve
Improve
Like Article
Like
Save
Share
Report

This uniqueSort() method in jQuery is used to sorts an array of DOM elements, in place with the duplicates removed.

Syntax:

jQuery.uniqueSort( array )

Parameters: The uniqueSort() method accepts only one parameter that is mentioned above and described below:

  • array: This parameter holds the Array of DOM elements.

Return Value: It returns the sorted array of DOM elements after removing the duplicates.

Example 1: In this example, the uniqueSort() method removes duplicate elements from the array of divs.




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>JQuery | uniqueSort() method</title>
    <script src=
    </script>
  
    <style>
        div {
            color: blue;
        }
    </style>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
  
    <h3>JQuery | uniqueSort() method</h3>
    <div></div>
    <div class="geek"></div>
    <div class="geek"></div>
    <div class="geek"></div>
    <div></div>
  
    <script>
        $(function () {
  
            var divs = $("div").get();
  
            divs = divs.concat($(".geek").get());
            $("div:eq(1)").text(
                "Sorts an array of DOM elements "
                + divs.length + " with the duplicates removed");
  
            divs = jQuery.uniqueSort(divs);
            $("div:eq(2)").text(
                "Sorts an array of DOM elements "
                + divs.length + " with the duplicates removed")
                .css("color", "red");
        })
    </script>
</body>
  
</html>


Output:

Example 2: In this example, the uniqueSort() method removes all duplicate elements from the array of p’s.




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>JQuery | uniqueSort() method</title>
  
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
  
    <h3>JQuery | uniqueSort() method</h3>
    <p></p>
    <p class="geek"></p>
    <p></p>
      
    <script>
        $(function () {
  
            var ps = $("p").get();
            ps = jQuery.uniqueSort(ps);
            $ UniqueSort(document getElementsByTagName("p".));
            $("p").text("Sorts an array of DOM elements "
                    + "with the duplicates removed")
                    .css("color", "red");
        })
    </script>
</body>
  
</html>


Output:



Last Updated : 27 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads