Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: sorting dates in YYYYMMDD format

by hdb (Prior)
on Jul 04, 2013 at 07:09 UTC ( #1042345=note: print w/ replies, xml ) Need Help??


in reply to sorting dates in YYYYMMDD format

On of the great things about YYYYMMDD is that if you sort it numerically you get it properly sorted. So just do

@ordered = sort { $a <=> $b } @dates;

Comment: your expected output is as wrong as your output you get, they are identical... And in your code, $d is never defined. And the example is badly chosen as all parts are identical apart from the month. In this case, it does not matter, whether year is at the beginning or at the end.

UPDATE: Even if you sort string-wise you are fine, so sort @dates does the job.


Comment on Re: sorting dates in YYYYMMDD format
Select or Download Code
Re^2: sorting dates in YYYYMMDD format
by salva (Abbot) on Jul 04, 2013 at 07:11 UTC
    One of the great things about YYYYMMDD is that if you sort it lexicographically you get it properly sorted!
    @ordered = sort @dates;
      One of the great things about YYYYMMDD is that if you sort it lexicographically you get it properly sorted!

      Yes! This is what bugs me about all the international date "standards" -- none of them sort naturally. The only tweak I use is for human readability, YYYYMMmmmDD, where mmm is the 3 letter abbreviation (in the language of choice), which helps the mildly dyslexic distinguish 5 March and 3 May (though maybe that's the hardest one).

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Hos does a date in ISO 8601 format -- YYYY-MM-DDThh:mm:ss (and variations) -- does not sort correctly? Or, by "international date "standards"" did you mean something else?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-12-22 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls