in reply to Sorting by Date Help!

my @stuff = ( {submitted => "01/22/08"}, {submitted => "01/12/08"}, {submitted => "04/01/09"}, {submitted => "10/04/08"}, {submitted => "01/10/09"}, {submitted => "12/12/09"}, {submitted => "01/22/09"}, {submitted => "01/12/09"}, {submitted => "12/30/08"}, {submitted => "12/22/08"}, {submitted => "12/15/08"}, {submitted => "01/05/09"}, {submitted => "01/19/09"}, {submitted => "01/21/09"} ); my @sorted = map { $_->[0] } sort { $b->[1] cmp $a->[1] } # map { print "$_->[1]\n"; $_ } # uncomment for debugging purposes map { [$_, join('', (split(/\//, $_->{submitted}))[2,0,1] )] } @stuff; foreach my $res (@sorted) { print $res->{submitted}."\n"; }

See Schwartzian Transform

Replies are listed 'Best First'.
Re^2: Sorting by Date Help!
by Anonymous Monk on Mar 17, 2009 at 20:35 UTC
    It worked, if you would have to explain this code to someone, would the "Schwartzian Transform" link be the place to start? Thanks for the help!
      would the "Schwartzian Transform" link be the place to start?

      Yes, I think so, as from there you'll also find a link to the original Unix Review column. The latter explains it in detail, but the PerlMonks thread provides additional discussion...

      What isn't explained there is the specific implementation of the "expensive_func" being used here.  So just in case :)

      join('', (split(/\//, $_->{submitted}))[2,0,1] )

      This splits the date strings by "/", rearranges the three parts so that year comes first, then month, then day (which makes the dates easily sortable by string comparison), and joins them into a single string like "080122" (for 01/22/08).  The (...)[2,0,1] construct is described in perldata (search for "list slice").