Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 version

Update 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;

In reply to Re: short sorts by Arunbear
in thread short sorts by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found