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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |