Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Created July 16, 2014 18:03
Show Gist options
  • Save jreisinger/41426c6cd1c0c49ec078 to your computer and use it in GitHub Desktop.
Save jreisinger/41426c6cd1c0c49ec078 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use v5.10; # to have say
# Get five random dates in the format MM-DD-YYYY
my @dates;
my $n = 0;
while ( $n++ <= 4 ) {
#<<<
push @dates, sprintf "%02d-%02d-%4d",
int( rand(12) ) + 1, # month
int( rand(31) ) + 1, # day
int( rand(114) ) + 1900; # year between 1900 and 2014
#>>>
}
my @sorted = sort {
my ( $ma, $da, $ya ) = split /-/, $a;
my ( $mb, $db, $yb ) = split /-/, $b;
$ya <=> $yb || $ma <=> $mb || $da <=> $db;
} @dates;
say "## Unsorted";
say for @dates;
say "## Sorted ascending";
say for @sorted;
say "## Sorted descending";
say for reverse @sorted;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment