I haven't had to do "special" sorts very often and I was quite pleased with the end result on this one- I think it is the most readable/understandable, whilst eliminating testing for the same thing (or it's negation) more than once and the temporary variable. I am sure some (or all) of the steps could have been skipped with experience, the answer looks obvious to me now but wasn't at the beginning.
# sort things containing a colon after things without # fist attempt if ( $a =~ /:/ ){ return $a cmp $b if $b =~ /:/; return 1; } if ( $b =~ /:/ ){ return -1; } $a cmp $b; # version 2, only one $a cmp $b return 1 if ( $a =~ /:/ and $b !~ /:/ ); return -1 if ( $b =~ /:/ and $a !~ /:/ ); $a cmp $b; # version 3 with input from a colleague, only apply a re to # each var once now if ( my $x = ($a =~ /:/) <=> ($b =~ /:/) ){ return $x; } $a cmp $b; # maybe better maybe not but it allowed me to see the # final option- my $x = ($a =~ /:/) <=> ($b =~ /:/); $x ? $x : $a cmp $b; # a good nights sleep and... (($a =~ /:/) <=> ($b =~ /:/)) || $a cmp $b;
--
Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Genesis of a sort routine
by edan (Curate) on Nov 06, 2003 at 08:53 UTC | |
by Abigail-II (Bishop) on Nov 06, 2003 at 10:32 UTC | |
by Abigail-II (Bishop) on Nov 06, 2003 at 10:59 UTC | |
by Anonymous Monk on Nov 06, 2003 at 14:30 UTC | |
by BrowserUk (Patriarch) on Nov 06, 2003 at 10:44 UTC | |
by bart (Canon) on Nov 06, 2003 at 18:38 UTC | |
by BrowserUk (Patriarch) on Nov 06, 2003 at 09:39 UTC | |
by edan (Curate) on Nov 06, 2003 at 09:50 UTC | |
by BrowserUk (Patriarch) on Nov 06, 2003 at 10:30 UTC | |
by sauoq (Abbot) on Nov 06, 2003 at 10:58 UTC | |
| |
by Anonymous Monk on Nov 06, 2003 at 09:01 UTC | |
|
Re: Genesis of a sort routine
by Anonymous Monk on Nov 06, 2003 at 07:34 UTC | |
|
Re: Genesis of a sort routine
by RMGir (Prior) on Nov 06, 2003 at 15:21 UTC | |
by greenFox (Vicar) on Nov 07, 2003 at 06:24 UTC | |
|
Re: Genesis of a sort routine
by Roger (Parson) on Nov 06, 2003 at 06:04 UTC |