Yes - this is VERY late to this party, but, this code should be relevant for reference.

I liked injunjoel's streightforward code, but I wanted to simplify it, and make special cases stand out. I also dislike handling special cases inside sort routines, because these are called more often than the number of items to be sorted. I also believe I have minimized the number of loops.

The only part I am not happy with is the "Special Marker" that is passed into the sort. That is a compromise made in order to simplify the code:

#!/usr/bin/perl -w use strict; 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 => 'lisa', title => 'Eating Veggies' }, { author => 'bart', title => 'coolness' } ); #this begs for a new structure. #lets use a hash. my (%authors_books, $FirstAuth); my $FirstTitle = "THIS BOOK FIRST"; my $Special_Low_Sort_Marker = "0x00" x 100; # A hundred nulls #turn your AoH's into a HoA's #Find first Author #Mark Specially-named books #see perldsc for reference foreach (@things_to_sort){ if ( $_->{title} eq $FirstTitle){ $FirstAuth = $_->{author} ; push @{$authors_books{ $_->{author} }}, $Special_Low_Sort_Marker . $_->{title}; # Special Marker - + Push to top of SORT }else{ push @{$authors_books{ $_->{author} }}, $_->{title}; } } #display the information. if ($FirstAuth){ PrintAuthor($FirstAuth); delete $authors_books{$FirstAuth}; } foreach my $author ( sort keys %authors_books){ PrintAuthor($author); } ######################## sub PrintAuthor{ my $author = shift; print "$author books:\n"; foreach (sort @{$authors_books{$author}}){ s/^$Special_Low_Sort_Marker//; # Remove Special marker print "\t$_\n"; } }
I added an extra data line, to ensure that sorting was alpha, according the OP's spec. The output:
lisa books: THIS BOOK FIRST Eating Veggies postmodernism bart books: coolness skateboarding homer books: donuts marge books: hairstyles
Extending this to allow Multiple "First Authors" is left as an exercise for the reader.

     "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken


In reply to Re: Twisted sort requirements by NetWallah
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.