in reply to Re^3: Twisted sort requirements
in thread Twisted sort requirements
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
|
|---|