jQuery .uniqueSort() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.uniqueSort() (since jQuery 3.7) is an instance method: you call it on a jQuery object, for example $("div").uniqueSort(). It sorts the matched elements into document order and removes duplicate DOM nodes (compared by node identity) in place, then returns the jQuery object so you can chain more methods.

Do not confuse it with the utility $.uniqueSort(array), which takes a plain array of DOM nodes. The instance method works directly on the collection — no .get() round trip.

Syntax

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

jQuery
$set.uniqueSort();                 // instance method (3.7+) → same jQuery object

// Chain right away
$set.uniqueSort().wrapAll("<div class='wrap'></div>");

// Utility version (different API): needs a plain array
var nodes = $.uniqueSort($set.get());
CallWhat it does
$set.uniqueSort()Sorts the collection in document order, drops repeated nodes, returns the jQuery object.
$.uniqueSort(array)Utility: sorts and de-duplicates a plain array of DOM nodes and returns that array.

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

Parameters

ParameterDescription
None.uniqueSort() takes no arguments. The collection you call it on is the input.

Return Value

Return ValueDescription
jQuery objectThe same collection, now in document order with duplicates removed, ready for chaining.

jQuery Examples

Example 1 — Collection With Duplicates

Build a collection that is out of order and lists some nodes twice, then clean it up.

jQuery
var a = document.getElementById("a");
var b = document.getElementById("b");
var c = document.getElementById("c");

var $set = $([c, a, c, b, a]);
console.log($set.length);   // 5
$set.uniqueSort();
console.log($set.length);   // 3  → a, b, c
Try It Yourself

How It Works

  1. Build. $([c, a, c, b, a]) keeps the array order and repeats.
  2. Dedupe. Repeated references to the same node are removed.
  3. Sort. The three nodes end up in document order: a, b, c.

Example 2 — wrapAll() Without uniqueSort()

With an out-of-order collection, .wrapAll() follows collection order, not page order.

jQuery
// #stage contains A, B, C
var $set = $([c, a]);            // C first, then A

$set.wrapAll("<div class='wrap'></div>");
Try It Yourself

How It Works

  1. First element. The wrapper is inserted before the first item in the collection — C.
  2. Append order. C is moved in first, then A, so the wrapper holds c, a.
  3. Result. B is left outside and the wrapped order is reversed from the page.

Example 3 — wrapAll() With uniqueSort()

Call .uniqueSort() first so the same collection is wrapped in document order.

jQuery
var $set = $([c, a]);

$set.uniqueSort().wrapAll("<div class='wrap'></div>");
Try It Yourself

How It Works

  1. Sort. .uniqueSort() turns [C, A] into [A, C].
  2. Chain. It returns the jQuery object, so .wrapAll() runs right after.
  3. Result. The wrapper sits where A was and holds a, c in page order.

Frequently Asked Questions

.uniqueSort() is an instance method (jQuery 3.7+) that sorts the elements in a jQuery collection into document order and removes duplicate DOM nodes. It changes the collection in place and returns the jQuery object so you can keep chaining.
$.uniqueSort(array) is a utility function: you pass it a plain array of DOM nodes. .uniqueSort() is called directly on a jQuery object — $("div").uniqueSort() — and works on that collection with no .get() conversion.
Only the exact same DOM node. Two different elements with the same class, text, or markup are both kept. jQuery compares node identity, not attributes.
No. The collection is sorted and de-duplicated in place and the same jQuery object is returned. Take a copy first (for example with $set.clone() on the elements, or $($set.get())) if you need to keep the original order.
Usually not. .add() already returns elements in document order without duplicates. .uniqueSort() matters when you build a collection by hand (for example $([nodeC, nodeA, nodeC])) or merge nodes with $.merge().
jQuery 3.7 or newer. On older versions, call the utility instead: $.uniqueSort($set.get()), then wrap the result with $().

Next: .children() Method

Get the direct child elements of each element in the set.

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

8 people found this page helpful