Ahhh! Thank you John! I was confused and thinking that sort was like an iterator, but it is not, it is operating on the whole collection returned by the $(...) search. And the pushStack makes sense now, so if I wanted the unsorted collection later you could reach into the stack and recover it presumably.
Thank you for the enlightenment :) -Steve John Resig wrote: > Sure. > > Let's start with this: > [].sort.apply( this, arguments ) > 'this' contains the current jQuery object, 'arguments' contains an > array of all the arguments passed in to the .sort() function. > > jQuery Is a pseudo-array (not a true array) so it doesn't support > operations like .sort(), natively. To mimic this, we get the sort > function from a regular array: > [].sort > and then call it within the context of our jQuery array using > JavaScript's apply method > [].sort.apply( this, ... ) > and pass in our arguments to it (which should be just a single > function, which is exactly what an array's sort function takes). > > This whole mess returns a new array of DOM elements. Which brings us > to the rest: > return this.pushStack( ..., []); > The pushStack function is part of jQuery, it allows it to keep a > stateful watch over what you match (it allows you to do .find() and > .end(), for example). In this case we want to make sure that .sort() > is stateful, since it's modifying the contents of the jQuery object. > > The second argument to pushStack is normally the arguments array - > this is so that you can do stuff like: > .filter(".foo",function(){ ... }) > but in this case, we don't have a function to pass into .sort(), so we > just provide an empty array. > > The result is a stateful way of sorting a set of DOM elements > contained within a jQuery object. It can be used like this: > > $("div.items").sort(function(a,b){ > return a.innerHTML > b.innerHTML ? 1 : -1; > }).remove().appendTo("#itemlist"); > > Hope this helps. > > --John > > On 10/19/06, Stephen Woodbridge <[EMAIL PROTECTED]> wrote: >> Ok, I am just not worthy! <sigh> >> Somebody please walk me through how this plugin works. >> >> -Steve >> >> John Resig wrote: >>> Hi Erik - >>> >>> With your particular example, give this plugin a try: >>> >>> jQuery.fn.sort = function() { >>> return this.pushStack( [].sort.apply( this, arguments ), []); >>> }; >>> >>> We've been talking about adding this for a little while now, so it may >>> become official: >>> http://jquery.com/dev/bugs/bug/255/ >>> >>> Using that plugin you can use your exact snippet, above. Let me know >>> if this helps. >>> >>> --John >>> >>>> I've been using jQuery for a month or so now and am really enjoying it! >>>> >>>> I haven't been able to figure out how to sort a collection of elements. I >>>> have something like the following: >>>> >>>> <div class="entry"><span class="foo">foo1</span><span >>>> class="bar">bar1</span></div >>>> <div class="entry"><span class="foo">foo2</span><span >>>> class="bar">bar2</span></div >>>> >>>> I would like to be able to do something like: >>>> >>>> $('.entry').sort(function(a, b) { return $('.foo', a).text()-$('.foo', >>>> b).text(); }).appendTo('#sortedStuff'); >>>> >>>> Or something like that. I know javascript has a sort function, but that's >>>> for arrays, so I'm not sure how that will work with jQuery? Also, a google >>>> search for 'jquery sort' (no quotes), brings up some stuff about sorting in >>>> the jQuery code, but it appears old and I can't find anything about sorting >>>> in the latest release. >>>> >>>> Lastly, jQarray.js seems like it would do what I want, but it appears that >>>> site is gone (taking jXs with it, btw). It would be nice if plugins were >>>> cached on the jQuery site. Anybody have jQarray/jXs? >>> _______________________________________________ >>> jQuery mailing list >>> discuss@jquery.com >>> http://jquery.com/discuss/ >> >> _______________________________________________ >> jQuery mailing list >> discuss@jquery.com >> http://jquery.com/discuss/ >> > > _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/