in reply to Sort text string by the date embedded
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'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort text string by the date embedded
by and_noel (Initiate) on Oct 17, 2011 at 17:19 UTC | |
|
Re^2: Sort text string by the date embedded
by AR (Friar) on Oct 17, 2011 at 16:53 UTC | |
by Marshall (Canon) on Oct 18, 2011 at 00:14 UTC |