Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I sort something by date?

by damian1301 (Curate)
on May 31, 2001 at 01:21 UTC ( #84383=perlquestion: print w/replies, xml ) Need Help??

damian1301 has asked for the wisdom of the Perl Monks concerning the following question: (sorting)

How would I sort something by date if the date's format were like so:

month/day/year

or

5/30/2001?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I sort something by date?
by merlyn (Sage) on May 31, 2001 at 01:30 UTC
    ## using a modified Schwartzian Transform, called the GSR sort... my @result = map { join "/", (/(....)(..)(..)/)[1,2,0]; } sort map { sprintf "%04d%02d%02d", (split /\//)[2,0,1]; } @input;
Re: How do I sort something by date?
by davorg (Chancellor) on May 31, 2001 at 12:24 UTC

    Also consider using a more sensible format for your dates. YYYY-MM-DD is much better because a) no-one will confuse months and days and b) you can sort it.

      Also, if you do this you can then use the native sort as described at http://www.perlmonks.org/?node_id=519149.
Re: How do I sort something by date?
by salva (Canon) on Apr 27, 2005 at 19:24 UTC
    If you are using DateTime objects, then you should considered using Sort::Key::DateTime for sorting.
      You need to add "using DateTime" to the title. Most people will just use timestamps for sorting by date, or a fixed-length date string.
Re: How do I sort something by date?
by St{) (Initiate) on Oct 20, 2002 at 04:57 UTC
    Actually the best idea would be to look at Schwartz's Learning Perl (3rd) book.

    It tells you in Chapter 15 how to make perl  sort according to what you want. The default is the (weird) ASCIIbetical method. In that method Caps come before lower case letters, and punctuation comes in all different places. By using Chapter 15's lesson you can make it sort to however YOU want it to (yes, even your date idea).

    <STDIN>

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-05-23 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?