Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: sorting dates in YYYYMMDD format

by learner@perl (Novice)
on Jul 04, 2013 at 07:17 UTC ( #1042351=note: print w/replies, xml ) Need Help??

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

Hi Masters.

I solved the problem

@dates = ('20130601', '20130401', '20130501'); my @sorted = map $_->[0], sort { $a->[1] cmp $b->[1] } map [ $_, join('', (/(..)(..)(....)/)[0,1,2]) ], @dates; # DD-MM-YYYY print "@sorted\n";


20130401 20130501 20130601

Thanks all

Replies are listed 'Best First'.
Re^3: sorting dates in YYYYMMDD format
by hdb (Monsignor) on Jul 04, 2013 at 07:22 UTC

    Do you realize, that

    join('', (/(..)(..)(....)/)[0,1,2])

    does not change a string $_ of length 8 in any way? (Apart from burning CPU cycles...). The regex also does not fit very well when splitting YYYYMMDD into pieces.

      Yes, but the real strings that have to be sorted may not be that easy, containing e.g. rubbish at start, or some punctuation, or - worse - be of different, yet regexpable format. (I don't know for sure, just a thought).
        Yes, but the real strings that have to be sorted may not be that easy ...

        Yes, but do you see that part of the point hdb was making was that  /(..)(..)(....)/ does not extract anything meaningful from a string like  '20130401' because the year winds up as '20' and '13', and the month and day fields wind up stuck together as '0401'. As hdb points out, this only works because all this mess is immediately stuck back together to form the original string — which is then sorted lexicographically, as any number of monks have recommended. The fact that learner@perl specifies test data in the  @dates array in YYYYMMDD format and has a subsequent comment
            @dates;  # DD-MM-YYYY
        further suggests that he or she does not firmly grasp what is going on.

        BTW, a date in the format  '2012-04-01' or  '2012/04/01' or with any other delimiter character or characters will lexi-sort perfectly well as it stands as long as the delimiter(s) at each position are constant!

Re^3: sorting dates in YYYYMMDD format
by 2teez (Vicar) on Jul 04, 2013 at 08:17 UTC

    ..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

    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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-06-21 12:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.