Your skill will accomplishwhat the force of many cannot PerlMonks

### Re: sorting mm/dd/yy

by InfiniteSilence (Curate)
 on Dec 15, 2000 at 21:40 UTC ( #46867=note: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Re: sorting mm/dd/yy
by merlyn (Sage) on Dec 15, 2000 at 21:43 UTC
Uh, no. Don't do this. You have duplicated the code for parsing \$a and \$b. Once you've done that, you run into maintenance headaches or worse, you could be treating them asymetrically, and on older versions of Perl this could actually dump core.

PLEASE DON'T copy this style at all. There have been other much better postings in this thread.

Re: Re: sorting mm/dd/yy
by chipmunk (Parson) on Dec 15, 2000 at 22:01 UTC
Your way of calling a special sorting routine really isn't the way that feature was designed to be used.

Instead of declaring a sort block that calls your sort routine explicitly, with \$a and \$b as args, you should write a sub that processes \$a and \$b directly, and just give the name of the sub to sort. Here's an example:

```    sub backwards { \$b cmp \$a }
sort backwards @a;

Create A New User
Node Status?
node history
Node Type: note [id://46867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-01-17 00:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (167 votes). Check out past polls.

Notices?