Ticket #255 (closed feature: invalid)
Add reverse and sort to jQuery core
| Reported by: | joern | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | core | Version: | |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by flesler) (diff)
reverse:
jQuery.fn.reverse = function() {
return this.pushStack(this.get().reverse(), arguments);
};
sort:
jQuery.fn.sort = function() {
return this.pushStack( [].sort.apply( this, arguments ), []);
};
Change History
comment:3 Changed 6 years ago by Erik
This breaks in jQuery 1.0.4 with the following error: second argument to Function.prototype.apply must be an array
Which happens down in set(), called from pushStack. Apparently, the array returned from apply isn't array enough to pass into apply down in set. I fixed it by wrapping the sort.apply() with merge:
jQuery.fn.sort = function() {
return this.pushStack( jQuery.merge( [].sort.apply( this, arguments ), []), [] );
};
I'm wondering if this is behavior that should be incorporated into the set() function? Are there other things expecting to pass not-quite-array objects to pushStack and have them dealt with properly?
comment:4 Changed 6 years ago by Erik
This also broke with 1.1, exactly the same way it did with 1.0.4. This seems to work with 1.1:
jQuery.fn.sort = function() {
return this.pushStack( jQuery.makeArray( [].sort.apply( this, arguments )) );
};
comment:5 Changed 6 years ago by john
- need set to Review
- Owner set to john
- Type set to feature
- Component set to core
- Priority set to minor
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Ticket #320 suggests a way to obviate these additional functions: having jQuery.fn inherit explicitly from Array.