Here is a possible solution with your own sort functio (this one sorts the dates in descending order, if you change the oder of the comparisons then you can easily get it to sort in ascending order).

#!/user/bin/perl use warnings; use strict; my @dates = ( '01-31 01:33', '01-30 22:10', '01-12 05:59', '01-02 01:33', '01-24 01:42', '01-12 05:56', ); print "The dates to be sorted are:\n"; my $numDates = scalar(@dates); my $colCount = 0; for(my $i = 0; $i < $numDates; $i++){ $colCount++; if($colCount > 3){ $colCount = 1; print "\n"; } # end if print " $dates[$i]"; } # end for $i loop print "\n"; my @sortedDates = sort mySort @dates; print "The sorted dates are:\n"; $numDates = scalar(@dates); $colCount = 0; for(my $i = 0; $i < $numDates; $i++){ $colCount++; if($colCount > 3){ $colCount = 1; print "\n"; } # end if print " $dates[$i]"; } # end for $i loop print "\n"; exit(0); sub mySort { my($part1a,$part2a) = split(/\s+/,$a); my($part1b,$part2b) = split(/\s+/,$b); my($mon_a,$day_a) = split(/-/,$part1a); my($mon_b,$day_b) = split(/-/,$part1b); my($hr_a,$min_a) = split(/:/,$part2a); my($hr_b,$min_b) = split(/:/,$part2b); return 0 if($a eq $b); if($mon_a == $mon_b) { if($day_a == $day_b) { if($hr_a == $hr_b) { return ($min_a <=> $min_b); } elsif($hr_a < $hr_b) { return -1; } else { return 1; } } elsif($day_a < $day_b) { return -1; } else { return 1; } } elsif($mon_a < $mon_b) { return -1; } else { return 1; } } # end sub mySort()

Note that I moved some of the dates in your @dates around so that you can see that it does, indeed, sort them.

ack Albuquerque, NM

In reply to Re: Sort by Date and Time by ack
in thread Sort by Date and Time by zod

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.