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 | |
by davido (Cardinal) on Feb 01, 2006 at 07:35 UTC | |
by davido (Cardinal) on Feb 01, 2006 at 08:31 UTC | |
by graff (Chancellor) on Feb 01, 2006 at 08:41 UTC |