jQuery.uniqueSort()S2C Home « Utilities « jQuery.uniqueSort()

Array object creation.

Description

The jQuery.uniqueSort() jQuery General utility method, returns an array of sorted DOM elements with duplicates removed.

Shorthand version $.unique()

  • The jQuery.uniqueSort() jQuery General utility method only works on arrays of DOM elements, not strings or numbers.
  • The returned array will always be in document order.

Syntax

Signature Description
jQuery.uniqueSort( array )Return an array of sorted DOM elements with duplicates removed.

Parameters

Parameter Description Type
arrayThe array of DOM elements.Array

Return

An Array object.

jQuery.uniqueSort( array ) ExampleTop

Return an array of sorted DOM elements with duplicates removed.

In the example below when we press the button we create an array variable of the table DOM elements on the page. There are three tables on the page comprised of the 'Syntax' and 'Parameter' tables above, both of which have a class of 'javatable' and the 'Other Tutorial Sites' table within the footer of the page. We then concatenate onto this array and output a message displaying the array length. After this we use the jQuery.uniqueSort() jQuery General utility method on the array, which removes the duplicates we added to the array. Finally we output a message displaying the array length again.



$(function(){
  $('#btn24').one('click', function(){
    var domElem = $('table').get();
    domElem = domElem.concat($('.javatable').get());
    $('#div20').append('<code>domElem</code> contains: ' + domElem.length + ' tables.<br>');
    domElem = jQuery.uniqueSort(domElem);
    $('#div20').append('<code>domElem</code> contains: ' + domElem.length + ' tables.<br>');
  });
});

div20. Some initial text.

Press the button below to action the above code: