jQuery uniqueSort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.uniqueSort(array) (alias $.uniqueSort) sorts an array or array-like list of DOM elements in place, removes duplicate node references, and returns that list in document order (added as uniqueSort in jQuery 1.12 / 2.2).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.uniqueSort(domArray)  → same array, sorted + deduped

// Duplicate = same node object (identity), not same HTML
// DOM elements only — not strings or numbers

// Official pattern:
divs = $("div").get().concat($(".dup").get())
divs = $.uniqueSort(divs)
CallWhat it does
uniqueSort()Learn jQuery.uniqueSort(): remove duplicate DOM node references, sort into document order, official merge demo, vs deprecated $.unique, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Build an array of DOM elements (.get() / .toArray()).
Step 2Optionally merge more nodes with $.merge or concat.
Step 3Call $.uniqueSort(arr) to dedupe and sort in place.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official Merge Pattern

Official jQuery API idea: concat overlapping selections, then uniqueSort.

jQuery
var divs = $("div").get();
divs = divs.concat($(".dup").get());

console.log(divs.length);       // e.g. 8 (6 + 2 overlaps)
divs = $.uniqueSort(divs);
console.log(divs.length);       // e.g. 6
Try It Yourself

How It Works

  1. First pass. $("div").get() collects every div once.
  2. Overlap. Concatenating $(".dup") adds some of those same nodes again.
  3. uniqueSort. Duplicate references are removed; order becomes document order.

Example 2 — Duplicate Node References

The same DOM node listed three times collapses to one.

jQuery
var target = document.getElementById("panel");
var nodes = [target, target, target];

console.log(nodes.length);  // 3
$.uniqueSort(nodes);
console.log(nodes.length);  // 1
Try It Yourself

How It Works

  1. One element. target is a single DOM node.
  2. Three slots. The array holds three references to that object.
  3. uniqueSort. Keeps one copy and sorts the array.

Example 3 — Look-Alike Nodes Stay Separate

Different elements with the same class are not duplicates.

jQuery
var items = $("li.item").get();  // three different <li> nodes

console.log(items.length);  // 3
$.uniqueSort(items);
console.log(items.length);  // 3  (still three nodes)
Try It Yourself

How It Works

  1. Three elements. Each <li class="item"> is its own DOM object.
  2. Same class ≠ same node. Attributes and text are ignored for dedupe.
  3. uniqueSort. Nothing is removed because no reference repeats.

Frequently Asked Questions

jQuery.uniqueSort(array) sorts an array or array-like list of DOM elements in place, removes duplicate node references, and returns that list in document order. It only works on DOM elements — not strings, numbers, or plain objects.
Two entries are duplicates only when they are the exact same DOM node object. Two different elements with identical class or text are not duplicates — jQuery compares node identity, not attributes.
They perform the same job. $.unique() was the original name. It was renamed to $.uniqueSort() (available as uniqueSort since 1.12/2.2) and $.unique became a deprecated alias in jQuery 3.0. Prefer $.uniqueSort() in new code.
jQuery.uniqueSort(array) is the utility — pass a native array (or array-like) of DOM nodes, e.g. var nodes = $("div").get(); $.uniqueSort(nodes);. Since jQuery 3.7, collections also have a separate .uniqueSort() instance method you call directly on the collection: $("div").uniqueSort(). See the .uniqueSort() traversing tutorial for that API.
No. For primitives, use a Set or Array.filter() instead. $.uniqueSort is a DOM-only helper, mainly used inside jQuery.
Rarely in app code — jQuery uses it internally. Reach for it when you merge node arrays from multiple selectors and need one de-duplicated list in document order.

Next: type() Method

See the deprecated alias name and how it maps to uniqueSort.

type() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful