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


In reply to Re^4: Twisted sort requirements by davido
in thread Twisted sort requirements by forrest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.