return this.pushStack( [].sort.apply( this, arguments ), []);
[] is an empty array, "[].sort" is the _javascript_ array sorting function. apply causes the sorting function to get called on this, with the arguments to our sort function as aruments to the native sort function. The whole sorted bit is added to the jquery stack. I'm not sure what the second [] argument is about (I'm pretty new to jQuery).
--Erik
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
> [email protected]
> http://jquery.com/discuss/
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/
_______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/
