Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Sort on Number Embedded in String

by RazorbladeBidet (Friar)
on Mar 22, 2005 at 19:16 UTC ( [id://441583]=note: print w/replies, xml ) Need Help??


in reply to Sort on Number Embedded in String

Wow, lots of replies, glad I didn't post mine :D

BUT - you have to make sure you're sorting on the right thing... you won't be if you just cmp on xxMarxxxx. You should grab the year, month and day and convert the month to the numeric equivalent and then format it as YYYYMMDD. Then <=> it

Update:
Took me a little bit to get the hang of it, but this works:
use strict; my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my %months; my @files = qw ( fwlog.14Mar2005.gz fwlog.15Mar2005.gz fwlog.16Mar2005.gz fwlog.17Mar2005.gz fwlog.18Mar2005.gz fwlog.19Mar2005.gz fwlog.1Mar2005.gz fwlog.20Dec2005.gz fwlog.21Mar2005.gz fwlog.2Mar2005.gz fwlog.3Mar2005.gz fwlog.4Mar2005.gz fwlog.5Jan2006.gz fwlog.6Mar2005.gz fwlog.7Mar2005.gz fwlog.8Mar2005.gz fwlog.9Mar2005.gz ); my $i = 1; $months{$_} = sprintf( "%02d", $i++ ) for @months; print $_, "\n" for map { $_->[0] } sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] or $a->[3] <=> $b->[3] } map { (split(/\./,$_,3))[1] =~ /^(\d+)([A-Za-z]+)(\d+)$/; [$_, sprintf( "%02d", $3), $months{$2}, $1 ] } @files;
--------------
It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://441583]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-09-20 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (26 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.