The jQuery.uniqueSort() utility removes duplicate DOM node references from an array and sorts the remaining elements into document order. This tutorial covers syntax, identity-based deduplication, five worked examples based on the official jQuery API demo, comparisons with $.merge(), and when jQuery.unique() was renamed.
01
Syntax
$.uniqueSort(arr)
02
DOM only
Elements, not strings
03
Identity
Same node ref
04
Doc order
Sorted in page
05
In place
Mutates array
06
Renamed
Was $.unique
Fundamentals
Introduction
When you combine DOM results from multiple jQuery selectors, the same element can appear more than once in your working array. jQuery provides jQuery.uniqueSort() (also written $.uniqueSort()) to clean that list: it strips duplicate node references and orders what remains according to where each element sits in the document.
This is a specialized utility. The jQuery API notes that you will probably never need it directly — jQuery calls it internally — but understanding it helps when you debug selector pipelines or manually merge node arrays before batch DOM updates.
Concept
Understanding the jQuery.uniqueSort() Method
jQuery.uniqueSort(array) accepts a native array (or array-like object) whose items are DOM elements. It modifies that array in place, removes entries that point to the same node object twice, and sorts survivors into document order. The return value is the same array reference, now shorter and ordered.
Duplicate detection is strict: only the exact same node counts as a duplicate. Two separate <div> elements with matching classes or ids remain distinct entries. This method does not deduplicate strings, numbers, or plain objects.
💡
Beginner Tip
Always convert jQuery collections to a plain array first with .get() or .toArray(). $.uniqueSort() expects a native array of nodes — not a jQuery object wrapper.
Foundation
📝 Syntax
General form of jQuery.uniqueSort:
jQuery
jQuery.uniqueSort( array )
// or
$.uniqueSort( array )
Parameters
array — a native array or array-like object containing DOM elements. This collection is modified in place.
Return value
Returns the same array reference, now deduplicated and sorted in document order.
Minimal workflow
jQuery
var nodes = $("div").get();
nodes = nodes.concat($(".highlight").get());
nodes = $.uniqueSort(nodes);
Cheat Sheet
⚡ Quick Reference
Goal
Code
Deduplicate DOM nodes
$.uniqueSort(nodes)
Convert jQuery collection
var nodes = $(sel).get()
Merge then dedupe
$.merge(a, b); $.uniqueSort(a)
Works on strings/numbers?
No — DOM elements only
Mutates array?
Yes — in place
Pre–jQuery 3 name
jQuery.unique()
Compare
📋 $.uniqueSort vs $.merge vs Set
Three tools that sound similar but solve different problems.
uniqueSort
DOM nodes
Remove duplicate node refs; document order
merge
append all
Concatenate arrays; keeps every entry
Set
primitives
Native dedupe for strings and numbers
Pipeline
merge+unique
Combine selections, then clean
Hands-On
Examples Gallery
Each example uses DOM elements and $.uniqueSort(). Open DevTools or use the Try-it links to run them interactively. Example 1 follows the official jQuery API demo pattern.
📚 Getting Started
Remove duplicate DOM references after combining node arrays.
Example 1 — Official jQuery API Demo Pattern
Collect all div elements, append duplicates from .dup, then run $.uniqueSort() to shrink the list.
jQuery
// uniqueSort() must take a native array
var divs = $("div").get();
// Add elements matched by .dup (they are divs too)
divs = divs.concat($(".dup").get());
console.log("Pre-uniqueSort:", divs.length);
divs = $.uniqueSort(divs);
console.log("Post-uniqueSort:", divs.length);
Some .dup nodes were already included in $("div"), so concatenating them adds the same node references again. $.uniqueSort() collapses those duplicates and orders the survivors as they appear in the document tree.
📈 Practical Patterns
Identity rules, look-alike elements, document order, and merge pipelines.
Example 2 — Duplicate References vs Unique Nodes
Push the same element twice manually — only one copy survives after uniqueSort.
jQuery
var target = document.getElementById("panel");
var nodes = [target, target, target];
console.log("Before:", nodes.length);
$.uniqueSort(nodes);
console.log("After:", nodes.length);
console.log(nodes[0] === target); // true
All three slots reference the identical DOM node object. uniqueSort keeps one entry because duplicates are detected by reference equality (===), not by comparing tag names or attributes.
Example 3 — Look-Alike Elements Are Not Duplicates
Two different <li> elements with the same text remain separate entries.
jQuery
var items = $("li.item").get();
console.log("Before:", items.length);
$.uniqueSort(items);
console.log("After:", items.length);
// Still 3 — three distinct node objects in the DOM
Even when elements look identical in HTML, each is a separate object in memory. uniqueSort does not compare innerHTML, classes, or ids — only whether two array slots point to the same node instance.
Example 4 — Sort into Document Order
Build an array in reverse DOM order, then let uniqueSort reorder it to match the page.
Since jQuery 1.4, uniqueSort always returns elements sorted by their position in the document. That predictable ordering matters when jQuery chains selector results before applying DOM operations.
Example 5 — Merge Selections, Then Deduplicate
Combine nodes from two selectors with $.merge, then clean the working buffer with $.uniqueSort.
Overlapping selectors often return the same anchor twice. Merge everything into one array for batch processing, then uniqueSort so each node is touched once when you wrap with $() and apply classes or events.
Applications
🚀 Common Use Cases
Combined selectors — dedupe after merging results from multiple jQuery queries.
Internal jQuery pipelines — understand how jQuery keeps DOM result sets clean.
Batch DOM updates — ensure each element is processed once after manual array assembly.
Plugin development — normalize node lists passed between callbacks in legacy jQuery plugins.
Document-order guarantees — rely on sorted output before iterating for layout-sensitive work.
Expect a native array or array-like object whose items are DOM elements.
Input
2
Sort by document position
Order elements according to their place in the DOM tree (since jQuery 1.4).
Sort
3
Remove duplicate refs
Drop later occurrences when two slots reference the same node object.
Dedupe
4
📦
Return same array
The modified array reference is returned with a shorter length.
Important
📝 Notes
Added in jQuery 1.12 and 2.2; renamed from jQuery.unique() in jQuery 3.0.
Since jQuery 1.4, results are always sorted in document order.
Only works on arrays of DOM elements — not strings, numbers, or generic objects.
Modifies the input array in place; clone first if you need the original list preserved.
Convert jQuery collections with .get() or .toArray() before calling.
Chiefly an internal jQuery helper — most application code never calls it directly.
Compatibility
Browser Support
jQuery.uniqueSort() ships with jQuery since 1.12 / 2.2 (as jQuery.unique() earlier). It works wherever jQuery runs — behavior is consistent because jQuery owns the implementation.
✓ jQuery 1.12+
jQuery jQuery.uniqueSort()
Available in jQuery 1.12+, 2.x, and 3.x as uniqueSort (3.0+) or unique (pre-3.0). Requires jQuery loaded; not a standalone browser API.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
jQuery.uniqueSort()Universal
Bottom line: Safe in any project that already includes jQuery. For deduplicating primitive values without jQuery, use Array.from(new Set(arr)) or .filter() with an index check.
Wrap Up
Conclusion
The jQuery.uniqueSort() utility cleans arrays of DOM elements by removing duplicate node references and sorting survivors into document order. Remember that deduplication is identity-based, the input array is mutated, and the method is specialized for DOM nodes — not general-purpose data cleaning.
Pair it with $.merge() when you combine selector results manually, and reach for native Set when you need to deduplicate strings or numbers instead.
Call .get() on jQuery collections before uniqueSort
Use after $.merge when selectors overlap
Clone the array first if you need the pre-dedupe list
Log length before/after when debugging selector overlap
Prefer jQuery’s built-in chaining when possible — it dedupes internally
❌ Don’t
Pass strings, numbers, or plain objects expecting deduplication
Expect look-alike elements with different node refs to merge
Pass a jQuery object directly — unwrap to a native array
Assume $.unique() and $.uniqueSort() differ in behavior
Use uniqueSort when $.grep or Set fits better
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about jQuery.uniqueSort()
Deduplicate DOM node arrays the jQuery way.
5
Core concepts
📝01
$.uniqueSort
DOM dedupe + sort
API
🔢02
Node identity
Same ref only
Rule
🗃03
Doc order
Sorted output
Since 1.4
⚠04
In place
Mutates array
Side effect
🔄05
Was unique
Renamed in 3.0
History
❓ Frequently Asked Questions
jQuery.uniqueSort(array) sorts an array or array-like collection of DOM elements in place, removes duplicate node references, and returns the same array sorted into document order. It only works on DOM elements — not strings, numbers, or plain objects.
Two entries are duplicates only when they reference the exact same DOM node object. Two different div elements with identical ids, classes, or text are not considered duplicates — jQuery compares node identity (===), not attributes or content.
They perform the same job. jQuery.unique() was the original name. It was renamed to jQuery.uniqueSort() in jQuery 3.0 to avoid confusion with other "unique" APIs. Older tutorials may still show $.unique().
No. Pass a native JavaScript array of DOM nodes. Convert a jQuery collection with .get() or .toArray() first: var nodes = $("div").get(); nodes = $.uniqueSort(nodes);
No. The method is designed for DOM element arrays and is chiefly used internally by jQuery. For primitive values, use Set, Array.filter(), or other native deduplication techniques.
Rarely in application code — jQuery uses it internally when combining DOM result sets. Reach for it when you merge node arrays from multiple selectors (via $.merge or concat) and need one de-duplicated list in document order before further DOM work.
Did you know?
Before jQuery 3.0 this method was called jQuery.unique(). The rename to uniqueSort() clarifies that results are sorted into document order — not merely filtered. Legacy codebases upgrading to jQuery 3 should search for $.unique( and update the name while keeping the same arguments.