in reply to sort based on date

If you are looking for speed, I'd recommend something like this (trading space for time + process your string using native functions):

my %h1 = ( 'Jan' => 'a', 'Feb' => 'b', ... 'Dec' => 'l', ); my %h2 = ( 'a' => 'Jan', 'b' => 'Feb', ... 'l' => 'Dec', ); my @sorted_dates = grep { s/^(.)(\d\d)/$e{$1} . ' ' . sprintf("%d",$2)/e } sort grep { s/^(.{3}) (\d\d?)/$d{$1} . sprintf("%02d",$2)/e; } @dates;

Of course you can/must rework the regex'es according to string format. Compared to code using Date::Manip, this one runs ~200 times faster (on my system)

Replies are listed 'Best First'.
Re^2: sort based on date
by Anonymous Monk on Aug 08, 2010 at 20:12 UTC
    EDIT: in regex'es $d must be $h1 and $e must be $h2
    my %h1 = ( 'Jan' => 'a', 'Feb' => 'b', ... 'Dec' => 'l', ); my %h2 = ( 'a' => 'Jan', 'b' => 'Feb', ... 'l' => 'Dec', ); my @sorted_dates = grep { s/^(.)(\d\d)/$h2{$1} . ' ' . sprintf("%d",$2)/e } sort grep { s/^(.{3}) (\d\d?)/$h1{$1} . sprintf("%02d",$2)/e; } @dates;

      grep? What are you trying to filter out?

      Anyway, the following manipulations are twice as fast:

      my %month_key_lookup = ( 'Jan' => 'a', 'Feb' => 'b', ... 'Dec' => 'l', ); my @sorted_dates = map substr($_, 12), sort map { /^(.{3}) (\d?)(. .*)/s; $month_key_lookup{$1}.($2||'0').$3.$_ } @dates;

      As a bonus, it doesn't clobber the contents of @dates.

        This was an idea, without knowing author's intentions (does he want to preserve the initial @dates or no, etc.) and without searching for the fastest combination of possibilities.

        Of course your exemple is better, it runs in ~65 microseconds on my system (compared to ~105 microseconds used by mine and ~20000 microseconds used by author's code). Anyway, TIMTOWTDI :)