jQuery uniqueSort() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is uniqueSort()?

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).

Remember
$.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)
NeedUse
Dedupe DOM nodes$.uniqueSort(arr)
Legacy name$.unique(arr) (deprecated)
Merge node arrays first$.merge(a, b)
Dedupe strings/numbersnew Set(...) (not $.uniqueSort)

How to Use uniqueSort()

Collect DOM nodes into a plain array, merge if needed, then uniqueSort.

  1. Build an array of DOM elements (.get() / .toArray()).
  2. Optionally merge more nodes with $.merge or concat.
  3. Call $.uniqueSort(arr) to dedupe and sort in place.
  4. Use the same array afterward (it is mutated).
PieceRequired?Role
arrayYesArray or array-like of DOM elements
Return—Same list, sorted + deduped (document order)
Mutation—In place — the input list is changed

Pseudocode

Pseudocode
function uniqueSort(array):
    // DOM elements only
    sort array into document order
    remove entries that are the same node object
    return array   // mutated in place

// Typical / official flow:
divs = $("div").get()
divs = divs.concat($(".dup").get())
divs = $.uniqueSort(divs)

Cheat Sheet

Keep these facts handy while deduping DOM node lists.

TopicDetail
SyntaxjQuery.uniqueSort(array) / $.uniqueSort(array)
SincejQuery 1.12 / 2.2 (as uniqueSort)
Old name$.unique() (deprecated alias in 3.0+)
InputArray / array-like of DOM elements only
Duplicate ruleSame node object (===)
OrderDocument order (since jQuery 1.4 behavior)
Merge helper$.merge()

Live Preview

Pick a sample node list, run $.uniqueSort, and inspect length / order.

Browser demo of $.uniqueSort() — same ideas as the examples below.

Result
sample = Official merge
before → 5
after  → 3
ids → us-a, us-b, us-c

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.
  4. Result. Length returns to the true unique node count.

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.
  4. In place. nodes itself shrinks — no new array is created.

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.
  4. Gotcha. Do not expect it to merge “similar looking” markup.

Example 4 — Sorted Into Document Order

Results are always returned in document order.

jQuery
var nodes = [
  document.querySelector("#third"),
  document.querySelector("#first"),
  document.querySelector("#second")
];

$.uniqueSort(nodes);

console.log(nodes.map(function (el) { return el.id; }).join(" → "));
// "first → second → third"
Try It Yourself

How It Works

  1. Shuffled input. Nodes start as third, first, second.
  2. Sort. $.uniqueSort reorders them as they appear in the document.
  3. No dupes here. Length stays 3; only order changes.
  4. Why. Document order makes later DOM walks predictable.

Edge Cases & Pitfalls

Check these before calling uniqueSort on real node lists.

DOM only

Not for strings or numbers

Pass DOM elements only. For primitives, use Set or filter — not $.uniqueSort.

identity

Same markup ≠ duplicate

Two different nodes with identical HTML are kept. Only repeated references to one node are removed.

array

Needs a native array / array-like

Do not pass a jQuery object. Convert with .get() or .toArray() first.

legacy

$.unique is the old name

In jQuery 3+, $.unique() is a deprecated alias of this method. Prefer $.uniqueSort().

Browser Support

jQuery.uniqueSort() is available as uniqueSort since jQuery 1.12 / 2.2 (earlier releases used $.unique()). It needs a browser DOM — not Node.js without a DOM polyfill.

✓ jQuery 1.12+ / 2.2+ · Browsers

jQuery.uniqueSort()

Works in Chrome, Firefox, Safari, and Edge with a supported jQuery version. Prefer $.uniqueSort() over deprecated $.unique(). DOM-only.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.uniqueSort() Browsers

Bottom line: Use $.uniqueSort(arr) in the browser with jQuery. For Node.js, use a DOM environment or a different approach.

Performance Notes

ApproachCostBest for
$.uniqueSort(arr)Sort + scanSmall/medium DOM node arrays
Let jQuery merge selectorsOften enoughNormal app code (rarely call uniqueSort yourself)
Huge node listsNoticeableAvoid re-running in hot loops

You rarely need this in application code — jQuery uses it internally. When you do, run it once after merging node arrays, not on every click.

Key Takeaways

  • Role: dedupe + sort DOM node arrays in place.
  • Identity: same node object only — not similar HTML.
  • Order: results are always in document order.
  • Name: prefer uniqueSort over deprecated $.unique.

One line: use $.uniqueSort for DOM identity dedupe in document order after merging node arrays — not as a general array unique.

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.
No. Pass a native array (or array-like) of DOM nodes. Convert with .get() or .toArray() first: var nodes = $("div").get(); $.uniqueSort(nodes);
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: unique()

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

unique() tutorial →

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