$.fn.sortByDepth = function() {
    var ar = [];
    var result = $([]);

    $(this).each(function() {
        ar.push({length: $(this).parents().length, elmt: $(this)});
    });
    ar.sort(function(a,b) {
        return b.length - a.length;
    });
    for (var i=0; i<ar.length; i++) {
        result.add(ar[i].elmt);
    };
    alert(result.length);
    return result;
};

In this function I try to create a jQuery collection from separate jQuery object. How can i do that ?

The code below doesn't work:

result.add(ar[i].elmt);

The jsfiddle: http://jsfiddle.net/hze3M/14/

Edit
Report