Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I sort something by date?

( #84383=categorized question: print w/ replies, xml ) Need Help??
Contributed by damian1301 on May 31, 2001 at 01:21 UTC
Q&A  > sorting


Description:

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

month/day/year

or

5/30/2001?

Answer: How do I sort something by date?
contributed by merlyn

## 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;
Answer: How do I sort something by date?
contributed by davorg

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.

Answer: How do I sort something by date?
contributed by salva

If you are using DateTime objects, then you should considered using Sort::Key::DateTime for sorting.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (15)
    As of 2014-07-22 19:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (126 votes), past polls