Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: How can I sort this array?

by flounder99 (Friar)
on Jun 17, 2002 at 19:24 UTC ( #175172=note: print w/replies, xml ) Need Help??

in reply to How can I sort this array?

Time::Local is core and faster than Date::* functions

use Time::Local qw( timelocal ); use strict; my @input = reverse qw( Order_29May11:05.bak Order_11Jun20:25.bak Order_10Jun14:16.bak Order_10Jun16:04.bak ); # reversed to prove it works since they are already sorted my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my %months; for (0 .. $#months) {$months{$months[$_]} = $_} # merlyn's definition of the # Schwartzian Transform # changed to numerical sort my @output = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, expensive_func($_)] } @input; print join "\n", @output; exit; sub expensive_func { # convert filename to date $_[0] =~ /Order_(\d+)(...)(\d+):(\d+)\.bak/ or die "bad filename $_[0]"; return timelocal(0, $4, $3, $1, $months{$2}, 0); } __OUTPUT___ Order_29May11:05.bak Order_10Jun14:16.bak Order_10Jun16:04.bak Order_11Jun20:25.bak
-- flounder

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://175172]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-03-19 09:43 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (238 votes). Check out past polls.