sub short_sorts {
my ($a,$b,$type) = @_;
# Legend:
# cs = case sensitive
# ci = case insensitive
# a = ascending
# d = descending
# r = reverse (right to left)
# n = numbers
# l = length of value
# Note:
# If you put $b from the main script into $a in this subroutine and use descending,
# you will actually get the list returned in ascending order.
my %sorts = (
'cs-a' => sub { $_[0] cmp $_[1] },
'cs-a-r' => sub { reverse($_[0]) cmp reverse($_[1]) },
'cs-d' => sub { $_[1] cmp $_[0] },
'cs-d-r' => sub { reverse($_[1]) cmp reverse($_[0]) },
'ci-a' => sub { uc $_[0] cmp uc $_[1] },
'ci-a-r' => sub { uc reverse($_[0]) cmp uc reverse($_[1]) },
'ci-d' => sub { uc $_[1] cmp uc $_[0] },
'ci-d-r' => sub { uc reverse($_[1]) cmp uc reverse($_[0]) },
'n-a' => sub { $_[0] <=> $_[1] },
'n-d' => sub { $_[1] <=> $_[0] },
'l-a' => sub { length($_[0]) <=> length($_[1]) },
'l-d' => sub { length($_[1]) <=> length($_[0]) },
);
return $sorts{$type}->($a,$b) if $type;
my $random_sort = (keys %sorts)[rand (keys %sorts)];
return $sorts{$random_sort}->($a,$b) if !$type;
}
####
my @unsorted_array = qw(red yellow green cyan blue magenta);
my @sorted_array = sort { short_sorts($a,$b,'ci-d-r") } @unsorted_array;
print "$_\n" for @sorted_array;
####
yellow
green
cyan
blue
red
magenta