#!/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 => 'bart', title => 'coolness' } ); #this begs for a new structure. #lets use a hash. my %authors_books; #turn your AoH's into a HoA's #see perldsc for reference foreach (@things_to_sort){ push @{$authors_books{$_->{author}}}, $_->{title}; } #sort the books for each author foreach my $author (keys %authors_books){ @{$authors_books{$author}} = sort{ $a eq "THIS BOOK FIRST" && -1 or $b eq "THIS BOOK FIRST" && 1 or $a cmp $b }@{$authors_books{$author}}; } #figure out who to display first. my @display_keys = sort{ (grep /THIS BOOK FIRST/, @{$authors_books{$a}}) && -1 or (grep /THIS BOOK FIRST/, @{$authors_books{$b}}) && 1 or $a cmp $b } keys %authors_books; #display the information. foreach my $display_key (@display_keys){ print "$display_key books:\n"; foreach (@{$authors_books{$display_key}}){ print "\t$_\n"; } } exit; #### lisa books: THIS BOOK FIRST postmodernism bart books: coolness skateboarding homer books: donuts marge books: hairstyles