Bug Tracker

Ticket #255 (closed feature: invalid)

Opened 2 years ago

Last modified 2 months ago

Add reverse and sort to jQuery core

Reported by: joern Assigned to: anonymous
Type: feature Priority: minor
Milestone: Component: core
Version: Keywords:
Cc: Needs: Review

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 ), []);
};

Attachments

Change History

Changed 2 years ago by henra..h+jquery@…

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

Changed 2 years ago by www.gewinnspieleplanet.com

!

Changed 2 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?

Changed 2 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 )) );

};

Changed 1 year ago by john

  • need set to Review
  • owner set to john
  • type set to feature
  • component set to core
  • priority set to minor

Changed 4 months ago by flesler

  • description changed from reverse: {{{ jQuery.fn.reverse = function() { return this.pushStack(this.get().reverse(), arguments); }; }}} sort: {{{ jQuery.fn.sort = function() { return this.pushStack( [].sort.apply( this, arguments ), []); }; }}} to reverse: {{{ jQuery.fn.reverse = function() { return this.pushStack(this.get().reverse(), arguments); }; }}} sort: {{{ jQuery.fn.sort = function() { return this.pushStack( [].sort.apply( this, arguments ), []); }; }}}

This has no use for the core, so shall I close this ?

Changed 2 months ago by flesler

  • status changed from new to closed
  • resolution set to invalid
Note: See TracTickets for help on using tickets.