in reply to Sorting an Array

the date format is 05/21/2014

Replies are listed 'Best First'.
Re^2: Sorting an Array
by Cristoforo (Curate) on Jul 31, 2014 at 16:43 UTC
    Haven't tried it out, but I think this would sort them.
    @statements = sort by_date @statements; sub by_date { my $dateA = $a->{statement_date}; my $dateB = $b->{statement_date}; $dateA =~ s!(\d\d)/(\d\d)/(\d\d\d\d)!$3$1$2!; $dateB =~ s!(\d\d)/(\d\d)/(\d\d\d\d)!$3$1$2!; $dateA cmp $dateB; }
        Or, you can use Time::Piece (core since 5.10) and Orcish Maneuver:
        my %cache; @sorted = sort { ( $cache{$a} //= 'Time::Piece'->strptime($a->{statement_date}, '%m +/%d/%Y') ) <=> ( $cache{$b} //= 'Time::Piece'->strptime($b->{statement_date}, '%m +/%d/%Y') ) } @statements;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ