Completely random sorting usually means there's something wrong with
your comparing function. Try adding some debugging output in your
comparing function and make sure you're comparing the correct values
and get the expected result there.

If you actually have null values in that first if statement, that
might lead to some seemingly random sorting since you return the same
thing when either one or the other or both are null. More accurate
would probably be to return 0 if a and b are both null, -1 if just a
is null, and 1 if just b is null (or visa versa). At least the null
values would get consistently sorted one way or the other.

But check what you're comparing. Break that really long return...
statement up into its parts and print out each part to make sure
you're comparing the right things.

--Erik


On 3/27/07, Chinmay Kulkarni <[EMAIL PROTECTED]> wrote:
>
> I tried using the code, as suggested by Eric, but it doesn't work. In my
> case, I'm trying to sort a bunch of divs based on the value of a span inside
> each. However, the sorting is completely random!
>
>  I don't know what I'm doing wrong!
>
> jQuery.noConflict();
> $j = jQuery;
>
> riggit = {
> /*...*/
>
>         init: function() {
>                 //jQuery extensions
>                 jQuery.fn.sort = function() {
>                         return this.pushStack( jQuery.makeArray( 
> [].sort.apply( this, arguments
> )) );
>                 };
>                 this.sortByScore($j('#post-0001'));   //Sample div id; One 
> among the
> sortable posts
>
>                ....
>
>         },
>
>         sortByScore: function(elem) {
>                 elem.siblings().sort( function(a,b) {
>                         if($j(a).find('.riggit-pointsDisplay').html() == null 
> ||
> $j(b).find('.riggit-pointsDisplay').html() == null) {
>                                 return 1;
>                         }
>                         return 
> (parseInt(($j(a).find('.riggit-pointsDisplay').html())) >
> parseInt($j(b).find('.riggit-pointsDisplay').html())) ? 1 : -1;
>                 }).appendTo(elem.parent());
>         },
> };
>
> $j(document).ready(function() {
>         riggit.init();
> });
>
> Would be most grateful for any help...
>
> Regards,
> Chinmay
>
>
>
> bmckenzie wrote:
> >
> > Thanks Eric.
> >
> > Erik Beeson said the following on 3/26/2007 9:47 AM:
> >> Even easier than that:
> >>
> >> jQuery.fn.sort = function() {
> >>     return this.pushStack( jQuery.makeArray( [].sort.apply( this,
> >> arguments )) );
> >> };
> >>
> >> See here: http://dev.jquery.com/ticket/255
> >>
> >> Looks like I'm still about the only person that actually uses this.
> >>
> > I'll probably use it quite a lot. My current app is where I've got a
> > bunch of forms that I want to rearrange themselves when a person submits
> > one with a changed date field. Doesn't seem that exotic to me :-)
> >
> > Bruce
> >
> > --
> > Bruce McKenzie
> > http://www.2MinuteExplainer.com
> >
> > _______________________________________________
> > jQuery mailing list
> > [email protected]
> > http://jquery.com/discuss/
> >
> >
>
> --
> View this message in context: 
> http://www.nabble.com/Sorting-items-in-jQuery-pseudo-array-tf3466978.html#a9690202
> Sent from the JQuery mailing list archive at Nabble.com.
>
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to