http://qs1969.pair.com?node_id=84383

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.