I mostly wrote the following as a tutorial to myself as to how Perl's sorting function works:
#!/usr/bin/perl -w
use strict;
#simple sort tests
my @a;
@a = (3,5,9,2,15,90,200);
print "Standard sort: " , join(" ",sort(@a));
print "\nCorreted numeric sort: ", join(" ", sort {$a <=> $b} @a);
#try dates
my @b = ('11/5/99','2/5/87','11/6/99');
print "\nUnsorted Dates: " , join(" ", @b) , "\n";
print "\nCustom Date Sort: ", join(" ", sort {date_sort($a, $b)} @b);
sub date_sort {
my ($first, $second) = @_;
if($first =~ m/(\d+)\/(\d+)\/(\d+)/g)
{
my (@mm, @dd, @yy);
($mm[1], $dd[1], $yy[1]) = ($1, $2, $3);
if ($yy[1] > 50) {$yy[1] += 1900} else {$yy[1] += 2000};
if($second =~ m/(\d+)\/(\d+)\/(\d+)/g)
{
($mm[2], $dd[2], $yy[2]) = ($1, $2, $3);
}
if ($yy[2] > 50) {$yy[2] += 1900} else {$yy[2] += 2000};
if ($yy[1] > $yy[2]) {return($first cmp $second)}
elsif ($yy[1] < $yy[2]) {return($second cmp $first)}
else {
#fall into testing month dates
if($mm[1] > $mm[2]) {return ($first cmp $second)}
elsif($mm[2] > $mm[1]) {return ($second cmp $first)}
else
{
#if that fails, test the days
if($dd[1] > $dd[2]) {return ($first cmp $second)}
else {return ($second cmp $first)}
}
}
;
}
}
1;
Quick explanation
The first and second sorts use Perl's standard sorting syntax. Nothing special here.
Perl also allows you to call a special sorting routine sort {date_sort($a, $b)} @b where the stuff between the curly braces is your call to the special function. The function basically checks the date and returns either one of two options...either $a is greater than $b or the reverse.
This is a very involved way to sort some bloody dates but, if you think about it, you could reuse the code if you needed a very, very specialized kind of date comparison function...e.g. perhaps something that seeks a particular range of dates and places them at the top of the sort order or something.
Oops...I tried a different set for the test data for this function and I am getting incorrect results. Don't use this for dates < 1950.
Celebrate Intellectual Diversity |