Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery jQuery.uniqueSort() Method

Posted in jQuery Tutorial
Updated on Nov 21, 2024
By Mari Selvan
👁️ 22 - Views
⏳ 4 mins
💬 1 Comment
jQuery jQuery.uniqueSort() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery offers a plethora of methods to simplify DOM manipulation and traversal. One such method is jQuery.uniqueSort(), which is invaluable for sorting and removing duplicate elements from an array of DOM elements.

In this guide, we'll explore the functionality of jQuery.uniqueSort() with practical examples to help you grasp its usage effectively.

🧠 Understanding jQuery.uniqueSort() Method

The jQuery.uniqueSort() method is designed to sort an array of DOM elements and remove any duplicate elements, ensuring that the resulting array contains only unique elements. It is particularly useful when dealing with collections of DOM elements and needing to eliminate redundancy.

💡 Syntax

The syntax for the jQuery.uniqueSort() method is straightforward:

syntax.js
Copied
Copy To Clipboard
jQuery.uniqueSort(array)

Parameters:

  • array: An array of DOM elements to be sorted and deduplicated.

Returns:

The method returns the sorted array with duplicate elements removed.

📝 Example

  1. Sorting and Removing Duplicates from an Array:

    Suppose you have an array of DOM elements containing duplicate entries, and you want to sort it and remove duplicates using jQuery.uniqueSort():

    example.js
    Copied
    Copy To Clipboard
    var elements = [document.getElementById("item1"), document.getElementById("item2"), document.getElementById("item1")];
    var uniqueElements = jQuery.uniqueSort(elements);
    console.log(uniqueElements);

    This will log the sorted array with duplicate elements removed.

  2. Applying uniqueSort() to jQuery Collections:

    You can also apply jQuery.uniqueSort() directly to jQuery collections. For instance:

    example.js
    Copied
    Copy To Clipboard
    var $items = $(".item");
    var uniqueItems = jQuery.uniqueSort($items.get());
    console.log(uniqueItems);

    This will log a sorted array of unique DOM elements matching the .item selector.

  3. Combining uniqueSort() with Other jQuery Methods:

    jQuery.uniqueSort() can be seamlessly integrated with other jQuery methods. For example, let's select all unique elements with a specific class and hide them:

    example.js
    Copied
    Copy To Clipboard
    var $uniqueItems = jQuery.uniqueSort($(".unique-item").get());
    $uniqueItems.forEach(function(element) {
      $(element).hide();
    });

    This will hide all unique elements with the class .unique-item.

🎉 Conclusion

The jQuery.uniqueSort() method is a powerful tool for sorting arrays of DOM elements and removing duplicates efficiently. Whether you're working with raw DOM elements or jQuery collections, this method ensures that you have a sorted array containing only unique elements.

By incorporating jQuery.uniqueSort() into your projects, you can streamline your DOM manipulation tasks and improve the efficiency of your code.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy