### comment on

 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

In reply to Re: sorting mm/dd/yy by InfiniteSilence
in thread sorting mm/dd/yy by electronicMacks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2021-01-17 00:50 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?