jQuery.uniqueSort()

Sorts an array or an array-like object of DOM elements, in place, with the duplicates removed. Note that this only works on arrays/array-likes of DOM elements, not strings or numbers.

jQuery.uniqueSort(array)👒 Array

array ArrayLikeObject The Array or an Array-like object of DOM elements.

The $.uniqueSort() function searches through an array or an array-like object of DOM elements, sorting the array/array-like, and removing any duplicate nodes. A node is considered a duplicate if it is the exact same node as one already in the input; two different nodes with identical attributes are not considered to be duplicates. This function only works on plain JavaScript arrays/array-like objects of DOM elements, and is chiefly used internally by jQuery. You probably will never need to use it.

Prior to jQuery 3.0, this method was called jQuery.unique() .

As of jQuery 1.4 the results will always be returned in document order.

Removes any duplicate elements from the array of divs.

JS
<div>There are 6 divs in this document.</div>
<div></div>
<div class="dup"></div>
<div class="dup"></div>
<div class="dup"></div>
<div></div>
CSS
div {
 color: blue;
}
HTML
// uniqueSort() must take a native array
var divs = $("div").get();
// Add 3 elements of class dup too (they are divs)
divs = divs.concat($(".dup").get());
$("div")
 .eq(1)
 .text("Pre-uniqueSort there are " + divs.length + " elements.");
divs = jQuery.uniqueSort(divs);
$("div")
 .eq(2)
 .text("Post-uniqueSort there are " + divs.length + " elements.")
 .css("color", "red");
DEMO

Looking for a Web Developer?

πŸ‘‹

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)