in reply to short sorts
Since you've already got ascending/descending options, passing in $a and $b seems redundant.
The options could be simplified by mimicking the regex flags e.g. assuming case sensitivity as the default i.e. 'i' meaning case insensitive and no 'i' meaning case sensitive. And also leaving out the hyphens.
It could be expanded by proving alternate equivalent options so you don't have to remember the order of them e.g. 'idr', 'rid' and 'dir' would all mean insensitive + descending + reversed.
Example:#!/usr/bin/perl use strict; use feature qw(switch say); use List::Util qw(shuffle); sub short_sorter { my ($type) = @_; # Legend: # i = case insensitive # a = ascending # d = descending # r = reverse (right to left) # n = numbers # l = length of value my %sorter = ( 'a' => sub { $a cmp $b }, 'd' => sub { $b cmp $a }, ); given ($type) { when ([qw[dr rd]]) { $sorter{$type} = sub { reverse($b) cmp re +verse($a) }; } when ([qw[ar ra]]) { $sorter{$type} = sub { reverse($a) cmp re +verse($b) }; } when ([qw[ai ia]]) { $sorter{$type} = sub { uc $a cmp uc $b }; + } when ([qw[di id]]) { $sorter{$type} = sub { uc $b cmp uc $a }; + } when ([qw[an na]]) { $sorter{$type} = sub { $a <=> $b }; } when ([qw[nd dn]]) { $sorter{$type} = sub { $b <=> $a }; } when ([qw[la al]]) { $sorter{$type} = sub { length($a) <=> len +gth($b) }; } when ([qw[ld dl]]) { $sorter{$type} = sub { length($b) <=> len +gth($a) }; } when ([qw[air ari iar ira rai ria]]) { $sorter{$type} = sub { uc reverse($a) cmp uc reverse($b) } +; } when ([qw[dir dri idr ird rdi rid]]) { $sorter{$type} = sub { uc reverse($b) cmp uc reverse($a) } +; } } if ($type) { return $sorter{$type} or die 'AAARGH!!'; } else { return (shuffle values %sorter)[0]; } } my @unsorted = qw(red lilac yelloW green cyan blue magenta); my $criteria = short_sorter('dir'); my @sorted = sort $criteria @unsorted; print "$_\n" for @sorted; __DATA__ yelloW green cyan blue red lilac magenta
Update
The random option doesn't do the right thing in this versionUpdate 2
Corrected with respect to random option:#!/usr/bin/perl use strict; use feature qw(switch say); use List::Util qw(shuffle); sub short_sorter { my ($type) = @_; # Legend: # i = case insensitive # a = ascending # d = descending # r = reverse (right to left) # n = numbers # l = length of value my %sorter = ( a => sub { $a cmp $b }, d => sub { $b cmp $a }, ai => sub { uc $a cmp uc $b }, di => sub { uc $b cmp uc $a }, an => sub { $a <=> $b }, dn => sub { $b <=> $a }, al => sub { length($a) <=> length($b) }, dl => sub { length($b) <=> length($a) }, ar => sub { reverse($a) cmp reverse($b) }, dr => sub { reverse($b) cmp reverse($a) }, air => sub { uc reverse($a) cmp uc reverse($b) }, dir => sub { uc reverse($b) cmp uc reverse($a) }, ); given ($type) { when ('dr') { $sorter{$type} = $sorter{dr} } when ('ra') { $sorter{$type} = $sorter{ar} } when ('ia') { $sorter{$type} = $sorter{ai} } when ('id') { $sorter{$type} = $sorter{di} } when ('na') { $sorter{$type} = $sorter{an} } when ('nd') { $sorter{$type} = $sorter{dn} } when ('la') { $sorter{$type} = $sorter{al} } when ('ld') { $sorter{$type} = $sorter{dl} } when ([qw[ari iar ira rai ria]]) { $sorter{$type} = $sorter{air}; } when ([qw[dri idr ird rdi rid]]) { $sorter{$type} = $sorter{dir}; } } if ($type) { return $sorter{$type} or die "Unknown options: $type"; } else { return (shuffle values %sorter)[0]; } } my @unsorted = qw(red lilac yelloW green cyan blue magenta); my $criteria = short_sorter('dri'); my @sorted = sort $criteria @unsorted; print "$_\n" for @sorted;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: short sorts
by Lady_Aleena (Priest) on Mar 30, 2012 at 18:39 UTC | |
by Arunbear (Prior) on Apr 03, 2012 at 17:49 UTC | |
by Lady_Aleena (Priest) on Apr 06, 2012 at 20:43 UTC | |
by Arunbear (Prior) on Apr 07, 2012 at 15:37 UTC | |
by topher (Scribe) on Apr 25, 2012 at 22:50 UTC | |
| |
by Lady_Aleena (Priest) on Apr 10, 2012 at 02:02 UTC |
In Section
Cool Uses for Perl