Your post would be much more readable if you enclosed the code withing <code>...</code tags.

The date format that you have is close to being able to be sorted by an alphanumeric comparison because you have 4 digit years and the months and days have leading zeroes (always are 2 digits).

So in the sort, just reorder the string into the right order and use a single cmp instruction.

#!/usr/bin/perl -w use strict; my @strings = ( "PROCESS_DT IN '01/01/2009'", "PROCESS_DT IN '05/23/2006'", "PROCESS_DT IN '01/01/2011'", "PROCESS_DT IN '04/19/2009'", "PROCESS_DT IN '07/01/2009'", ); @strings = sort { my ($monthA, $dayA, $yearA) = $a =~ m|(\d+)/(\d+)/( +\d+)|; my ($monthB, $dayB, $yearB) = $b =~ m|(\d+)/(\d+)/( +\d+)|; "$yearA$monthA$dayA" cmp "$yearB$monthB$dayB +" }@strings; print join("\n",@strings),"\n"; __END__ PROCESS_DT IN '05/23/2006' PROCESS_DT IN '01/01/2009' PROCESS_DT IN '04/19/2009' PROCESS_DT IN '07/01/2009' PROCESS_DT IN '01/01/2011'

In reply to Re: Sort text string by the date embedded by Marshall
in thread Sort text string by the date embedded by and_noel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.