Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: sorting dates in YYYYMMDD format

by 2teez (Priest)
on Jul 04, 2013 at 08:17 UTC ( #1042377=note: print w/ replies, xml ) Need Help??


in reply to Re^2: sorting dates in YYYYMMDD format
in thread sorting dates in YYYYMMDD format

..I solved the problem..
Good... but why not just use sort just as you have been told previously on this post.
Yes your regexes matched successfully, see

use re 'debug'; @dates = ('20130601', '20130401', '20130501'); my @sorted = map $_->[1], sort { $a->[1] <=> $b->[1] } map [ $_, join('', (/(..)(..)(....)/)[0,1,2]) ], @dates; # DD-MM-YYYY print "@sorted\n";
...produces...
Compiling REx `(..)(..)(....)' size 21 Got 172 bytes for offset annotations. first at 3 1: OPEN1(3) 3: REG_ANY(4) 4: REG_ANY(5) 5: CLOSE1(7) 7: OPEN2(9) 9: REG_ANY(10) 10: REG_ANY(11) 11: CLOSE2(13) 13: OPEN3(15) 15: REG_ANY(16) 16: REG_ANY(17) 17: REG_ANY(18) 18: REG_ANY(19) 19: CLOSE3(21) 21: END(0) minlen 8 Offsets: [21] 1[1] 0[0] 2[1] 3[1] 4[1] 0[0] 5[1] 0[0] 6[1] 7[1] 8[1] 0[0] 9[1] 0 +[0] 10[1] 11[1] 12[1] 13[1] 14[1] 0[0] 15[0] Matching REx `(..)(..)(....)' against `20130601' Setting an EVAL scope, savestack=12 0 <> <20130601> | 1: OPEN1 0 <> <20130601> | 3: REG_ANY 1 <2> <0130601> | 4: REG_ANY 2 <20> <130601> | 5: CLOSE1 2 <20> <130601> | 7: OPEN2 2 <20> <130601> | 9: REG_ANY 3 <201> <30601> | 10: REG_ANY 4 <2013> <0601> | 11: CLOSE2 4 <2013> <0601> | 13: OPEN3 4 <2013> <0601> | 15: REG_ANY 5 <20130> <601> | 16: REG_ANY 6 <201306> <01> | 17: REG_ANY 7 <2013060> <1> | 18: REG_ANY 8 <20130601> <> | 19: CLOSE3 8 <20130601> <> | 21: END Match successful! Matching REx `(..)(..)(....)' against `20130401' Setting an EVAL scope, savestack=12 0 <> <20130401> | 1: OPEN1 0 <> <20130401> | 3: REG_ANY 1 <2> <0130401> | 4: REG_ANY 2 <20> <130401> | 5: CLOSE1 2 <20> <130401> | 7: OPEN2 2 <20> <130401> | 9: REG_ANY 3 <201> <30401> | 10: REG_ANY 4 <2013> <0401> | 11: CLOSE2 4 <2013> <0401> | 13: OPEN3 4 <2013> <0401> | 15: REG_ANY 5 <20130> <401> | 16: REG_ANY 6 <201304> <01> | 17: REG_ANY 7 <2013040> <1> | 18: REG_ANY 8 <20130401> <> | 19: CLOSE3 8 <20130401> <> | 21: END Match successful! Matching REx `(..)(..)(....)' against `20130501' Setting an EVAL scope, savestack=12 0 <> <20130501> | 1: OPEN1 0 <> <20130501> | 3: REG_ANY 1 <2> <0130501> | 4: REG_ANY 2 <20> <130501> | 5: CLOSE1 2 <20> <130501> | 7: OPEN2 2 <20> <130501> | 9: REG_ANY 3 <201> <30501> | 10: REG_ANY 4 <2013> <0501> | 11: CLOSE2 4 <2013> <0501> | 13: OPEN3 4 <2013> <0501> | 15: REG_ANY 5 <20130> <501> | 16: REG_ANY 6 <201305> <01> | 17: REG_ANY 7 <2013050> <1> | 18: REG_ANY 8 <20130501> <> | 19: CLOSE3 8 <20130501> <> | 21: END Match successful! 20130401 20130501 20130601 Freeing REx: `"(..)(..)(....)"'
but even at that it a lot of work.
However, if you must use "Schwartzian transform", you can sort on the month, since the year is the same like this:
use warnings; use strict; my @dates = ('20130601', '20130401', '20130501'); print join ' ' => map{$_->[0]} sort{$a->[1] <=> $b->[1]} map{/.{4}(.{2})/;[$_,$1]} @dates;
But sincerely, for this use sort just like other monks told you.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re^3: sorting dates in YYYYMMDD format
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-10-31 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (219 votes), past polls