Last active
December 14, 2015 07:49
-
-
Save foxiepaws/5053708 to your computer and use it in GitHub Desktop.
Sorting files and stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Filesort | |
# Takes two Arguments, Requires one | |
# * $temp - [REQUIRED] Reference to an Array. | |
# * $opts - [Optional] Sets options | |
# * dir - Path to directory if not $PWD. | |
# * sortMode - Override the global sort mode. | |
# Returns an Array reference that is a sorted array. | |
# usage: sortFiles(\@temp,{dir => "$mydir",sortMode => "alpha"}); | |
sub sortFiles { | |
my $temp = shift; | |
my $opts = shift; | |
my $dir; | |
my $lsortMode; | |
# if we have options passed as well, check it. | |
if ($opts) { | |
if ($opts->{dir}) { | |
print "Set Dir\n"; | |
$dir = $opts->{dir}; | |
} else { | |
print "PWD\n"; | |
$dir = "."; | |
} | |
if ($opts->{sortMode}) { | |
$lsortMode = $opts->{sortMode}; | |
} elsif ($sortMode) { # we have a global sortmode. | |
$lsortMode = $sortMode; | |
} else { # we have nothing passed and the sortmode is undef! | |
$lsortMode = "default"; | |
} | |
} | |
given ($lsortMode) { | |
when("alpha") { | |
return sort @$temp | |
} | |
when("bymod") { | |
return sort { my ($a_dev,$a_ino,$a_mode,$a_nlink,$a_uid,$a_gid,$a_rdev,$a_size,$a_atime,$a_mtime,$a_ctime,$a_blksize,$a_blocks) = stat("$dir/$a"); my ($b_dev,$b_ino,$b_mode,$b_nlink,$b_uid,$b_gid,$b_rdev,$b_size,$b_atime,$b_mtime,$b_ctime,$b_blksize,$b_blocks) = stat("$dir/$b");return $a_mtime <=> $b_mtime; } @$temp; | |
} | |
when("bysize") { | |
return sort { my ($a_dev,$a_ino,$a_mode,$a_nlink,$a_uid,$a_gid,$a_rdev,$a_size,$a_atime,$a_mtime,$a_ctime,$a_blksize,$a_blocks) = stat("$dir/$a"); my ($b_dev,$b_ino,$b_mode,$b_nlink,$b_uid,$b_gid,$b_rdev,$b_size,$b_atime,$b_mtime,$b_ctime,$b_blksize,$b_blocks) = stat("$dir/$b");return $a_size <=> $b_size; } @$temp; | |
} | |
default { | |
return @$temp | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment