7

I was reading Learning jQuery 1.3 (Jonathan Chaffer and Karl Swedberg) and while sorting table, they used .get() before calling .sort(), and said

we need to transform jQuery objects into array of DOM nodes. Even though jQuery objects act like arrays in many respects , they don't have any of the native array methods available, such as .sort().

Code:

$("#sort").click(function() {
        var posts = $("#posts_div .post");
        posts.sort(function(a, b) {
           return ($(a).text()) > ($(b).text());
        });       
        $.each(posts, function(index, post) { $("#posts_div").append(post); });
});​

So I tried to do it without using .get(), but surprise it worked even without .get() with latest jQuery, but didn't work with 1.3

So made some fiddles to make it clear

**Not working without .get() jquery 1.2.6 **

Working with .get() jquery 1.2.6

Working without .get() jquery 1.7.2

Working with .get() jquery 1.7.2

So obviously earlier jQuery objects didn't used to have .sort() function same as Javascript arrays? But now they have..

So my question is what are the functions the jQuery objects not support yet, so we can keep in mind to convert to Javascript arrays, before use??

8
  • 2
    And another reason I don't use jQuery. Commented Jun 22, 2012 at 14:26
  • To address the last point: No, it's not a jQuery object. The overall thing is a jQuery object which contains DOM elements, so a and b (being individual elements inside the object) are DOM elements. Commented Jun 22, 2012 at 14:31
  • 1
    You shouldn't rely on undocumented features of any library--they could change at any minute. In this specific case, just go ahead and get a true array of DOM elements before performing array operations. Commented Jun 22, 2012 at 14:32
  • @AnthonyGrist when we have some selector returning multiple elements is it not a array of jquery objects?? Commented Jun 22, 2012 at 14:39
  • @RajatSinghal No. The .get() function is intended to provide you with access to the DOM elements themselves, so the array returned is an array of DOM elements, not jQuery objects. Commented Jun 22, 2012 at 14:53

2 Answers 2

4

jQuery objects currently support 3 array methods:

var methods = 'pop push reverse shift sort splice unshift concat join slice toString indexOf lastIndexOf filter forEach every map some reduce reduceRight'.split(' ')
var implemented = $.grep(methods, function(m) {
    return $.prototype[m] == Array.prototype[m];
});
console.log(implemented); // => ["push", "sort", "splice"]

They also have slice, but it's not the same slice as arrays have:

$.prototype.slice === Array.prototype.slice // => false
Sign up to request clarification or add additional context in comments.

Comments

1

jQuery does have a .sort method, it just isn't officially documented because it does not follow the usual format of jQuery methods.

The only methods that are supported are the ones listed in the api.

.sort is implemented as:

$.fn.sort = [].sort;

You can add your own additional array methods as needed in the same way.

$.fn.reverse = [].reverse;

If .sort isn't implemented in your version of jQuery, implement it yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.