in reply to Twisted sort requirements

This should do it:

use strict; use warnings; use Data::Dumper; my @things_to_sort = ( { author => 'bart', title => 'skateboarding' }, { author => 'lisa', title => 'postmodernism' }, { author => 'marge', title => 'hairstyles' }, { author => 'lisa', title => 'THIS BOOK FIRST' }, { author => 'homer', title => 'donuts' }, { author => 'bart', title => 'coolness' } ); my @sorted = sort { $a->{title} eq "THIS BOOK FIRST" && -1 or $b->{title} eq "THIS BOOK FIRST" && 1 or $a->{author} cmp $b->{author} or $a->{title} cmp $b->{title} } @things_to_sort; print Dumper \@sorted;

This uses logical short-circuiting to "fall through" until one of the comparisons produces a result. First, $a is tested to see if it has the key phrase. If not, then $b is tested. If not, then $a and $b are compared to see if there is inequality in author names. If so, great, but if not, fall through to the title comparison.


Dave

Replies are listed 'Best First'.
Re^2: Twisted sort requirements
by forrest (Beadle) on Feb 01, 2006 at 07:25 UTC
    That's the kind of thing I'm looking for, but you've missed the part of the requirement that after I get "THIS BOOK FIRST" I need all books by the same author to follow immediately after that (and finally the rest).

    I guess that might not be possible with a simple sort call.

      It is definately possible.

      graff kindly /msg'ed me to let me know I missed a specification. Update: I have now re-written my original method in such a way that the author is stored for future comparisons if title is "THIS BOOK FIRST". I then rewrote the logic into basic 'if' conditionals so that I could visualize the flow a little better. But I guess I've stared at it too long, and am simply missing something that someone's going to spot quickly. Unfortunately, that "something" is eluding me. Here is my code so far, that doesn't result in the correct sort order. I hope someone spots my flaw because it's going to keep me awake tonight.

      use strict; use warnings; use Data::Dumper; my @things_to_sort = ( { author => 'bart', title => 'skateboarding' }, { author => 'lisa', title => 'postmodernism' }, { author => 'marge', title => 'hairstyles' }, { author => 'lisa', title => 'THIS BOOK FIRST' }, { author => 'homer', title => 'donuts' }, { author => 'bart', title => 'coolness' } ); my @sorted; my $author = ''; @sorted = sort { if ( $a->{title} eq "THIS BOOK FIRST" ) { $author = $a->{author}; return -1; } elsif ( $b->{title} eq "THIS BOOK FIRST" ) { $author = $b->{author}; return 1; } elsif ( $a->{author} eq $author and $b->{author} eq $author ) { return( $a->{title} cmp $b->{title} ); } elsif ( $a->{author} eq $author ) { return -1; } elsif ( $b->{author} eq $author ) { return 1; } else { return ( $a->{author} cmp $b->{author} or $a->{title} cmp $b->{title} ) } } @things_to_sort; print Dumper \@sorted;

      I know it's really ugly. The "if" statements tend to do that, but I converted over to "if"s to help in visualizing the flow. ...obviously it didn't help me see the problem. I can't wait to hear if anyone else can spot it.


      Dave

        I'm choosing to follow up to my own node, because I believe the lesson learned is worth repeating for the benefit of others who may find the pitfall that tripped me up.

        The problem with my above code is that $author doesn't get set right away; it takes a few sort comparison iterations before $author gets set, and by then the order has already began to take shape. This resulted in one of the "lisa" entries being stranded near the bottom of the list, when it needed to find its way up to the top of the list. The solution is (as BrowserUk already pointed out further down in this thread, but which I errantly believed was unnecessary) to predetermine who the key author is, before starting the sort routine. The following code does finally work, and is ready to be tidied up into a more Perlish format:

        use strict; use warnings; use Data::Dumper; my @things_to_sort = ( { author => 'bart', title => 'skateboarding' }, { author => 'lisa', title => 'postmodernism' }, { author => 'marge', title => 'hairstyles' }, { author => 'lisa', title => 'THIS BOOK FIRST' }, { author => 'homer', title => 'donuts' }, { author => 'bart', title => 'coolness' } ); my @sorted; my $author; foreach my $thing ( @things_to_sort ) { if( $thing->{title} eq "THIS BOOK FIRST" ) { $author = $thing->{author}; last; } } @sorted = sort { if ( $a->{title} eq "THIS BOOK FIRST" ) { return -1; } elsif ( $b->{title} eq "THIS BOOK FIRST" ) { return 1; } elsif ( $a->{author} eq $author and $b->{author} eq $author ) { return( $a->{title} cmp $b->{title} ); } elsif ( $a->{author} eq $author ) { return -1; } elsif ( $b->{author} eq $author ) { return 1; } else { return ( $a->{author} cmp $b->{author} or $a->{title} cmp $b->{title} ) } } @things_to_sort; print Dumper \@sorted;

        And here is the sort routine re-written with the same logic but in what some might consider a more familiar sort idiom:

        @sorted = sort { $a->{title} eq "THIS BOOK FIRST" && -1 or $b->{title} eq "THIS BOOK FIRST" && 1 or ( $a->{author} eq $author and $b->{author} eq $author ) && $a->{title} cmp $b->{title} or $a->{author} eq $author && -1 or $b->{author} eq $author && 1 or $a->{author} cmp $b->{author} or $a->{title} cmp $b->{title} } @things_to_sort;

        I love this question. ++ to the OP. Thanks for puzzler, seriously!


        Dave

        Um, I think the problem here is: if you don't know the author of "THIS BOOK FIRST" before the sorting begins, then you effectively end up changing the sort conditions in the middle of the sorting process, which would tend to make that process somewhat unstable at best.

        I'll confess that I've forgotten most of the (relatively few) details I've ever known about sorting implementations, but I have a strong hunch that changing the sort criteria upon seeing input item N (where N is something other than "the first") would violate an essential underlying assumption that is built into most sort algorithms.