Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Sort an array of strings based on two fields

by hdb (Monsignor)
on Mar 14, 2013 at 10:50 UTC ( #1023457=note: print w/replies, xml ) Need Help??


in reply to Re: Sort an array of strings based on two fields
in thread Sort an array of strings based on two fields

As a small variant, you can calculate a key by 100*year+month, assuming that month will always be two digit at most...
my @sorted = sort { ($a =~ /\.([0-9]+)-([0-9]+)\./, 100*$1+$2) <=> ($b =~ /\.([0-9]+)-([0-9]+)\./, 100*$1+$2) } @strings;
or would it be ok to insert the missing zero?
map { s/-(\d)\./-0\1./ } @strings; my @sorted = sort @strings;
(Make sure the regexp does not match elsewhere in your strings.)

Replies are listed 'Best First'.
Re^3: Sort an array of strings based on two fields
by choroba (Bishop) on Mar 14, 2013 at 10:55 UTC
    I assumed 3 digits in the second solution. 50 seems too big for a months to me (maybe a week?)
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-01-22 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (235 votes). Check out past polls.

    Notices?