#!/usr/bin/env perl
use strict;
use warnings;

@ARGV or die "Usage: $0 string1 string2 string3 [etc]\n";

my @alpha_strings = sort by_alpha @ARGV;
print "Strings sorted alphabetically: @alpha_strings\n\n";


######
#Fill in the by_alpha subroutine we just used here:
######
sub by_alpha {

   lc $a cmp lc $b

}

my ($dir) = glob("~lallip/public/ica2");
opendir my $dh, $dir or die "Cannot open directory $dir: $!\n";
my @files;
while (my $file = readdir($dh)) {
   if (-f "$dir/$file") {
       push @files, "$dir/$file";
   }
}

my @sorted_files = sort by_size @files;

print "Files sorted smallest to largest: \n";
for my $path (@sorted_files) {
   my @path = split '/', $path;
   print "$path[$#path]\n";
}

print "\n";

######
#Fill in the by_size subroutine here:
######
sub by_size {

   -s $a <=> -s $b

}

my @length_strings = sort by_length @ARGV;
print "Strings sorted by length: @length_strings\n\n";

######
#Fill in the by_length subroutine here:
#####
sub by_length {

   length($b) <=> length($a)

}


#########################
# Complete only the above for full credit
# Uncomment and Complete the below for 0.5 bonus points
#########################

my @sorted_strings = sort by_complicated_sort @ARGV;
print "Strings sorted by position of 'a', length, and ASCIIbetically: @sorted_strings\n";

######
#Fill in the complicated sort here:
######
sub by_complicated_sort {

   index($a, 'a') <=> index($b, 'a')
   or
   length($a) <=> length($b)
   or
   $a cmp $b

}
