Created
November 29, 2016 07:46
-
-
Save chankeypathak/4fbcfcca0e93bd765e070027b5229984 to your computer and use it in GitHub Desktop.
Sort dates using Schwartzian transform in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Time::Piece; | |
my @events = ( '11/17/1999', '12/6/1999', '12/23/1999', | |
'1/23/2000', '1/13/2000', '2/25/2000', | |
'1/5/2000', '3/18/2000', '4/10/2000', | |
'3/12/2000', '12/31/1999'); | |
my @sorted_events = map { $_ -> [0] } | |
sort { | |
# year | |
$a -> [1] -> [2] <=> $b -> [1] -> [2] || | |
# month | |
$a -> [1] -> [0] <=> $b -> [1] -> [0] || | |
# day | |
$a -> [1] -> [1] <=> $b -> [1] -> [1] | |
} | |
map { [ $_, [ split /\// ] ] } | |
@events; | |
# you can also use Time::Piece | |
my @sorted_events_again = map { $_ -> [1] } | |
sort { $a -> [0] <=> $b -> [0] } | |
map { [ Time::Piece -> strptime($_, "%m/%d/%Y"), $_ ] } | |
@events; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment