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.
In reply to Re: Sort by Date and Time
by ack
in thread Sort by Date and Time
by zod
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |