It is definately possible.
graff kindly /msg'ed me to let me know I missed a specification. Update: I have now re-written my original method in such a way that the author is stored for future comparisons if title is "THIS BOOK FIRST". I then rewrote the logic into basic 'if' conditionals so that I could visualize the flow a little better. But I guess I've stared at it too long, and am simply missing something that someone's going to spot quickly. Unfortunately, that "something" is eluding me. Here is my code so far, that doesn't result in the correct sort order. I hope someone spots my flaw because it's going to keep me awake tonight.
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 = ''; @sorted = sort { if ( $a->{title} eq "THIS BOOK FIRST" ) { $author = $a->{author}; return -1; } elsif ( $b->{title} eq "THIS BOOK FIRST" ) { $author = $b->{author}; 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;
I know it's really ugly. The "if" statements tend to do that, but I converted over to "if"s to help in visualizing the flow. ...obviously it didn't help me see the problem. I can't wait to hear if anyone else can spot it.
Dave
In reply to Re^3: Twisted sort requirements
by davido
in thread Twisted sort requirements
by forrest
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |