ginju has asked for the wisdom of the Perl Monks concerning the following question:

Hello Great Monks,

I am parsing an excel file and getting a column of dates. I store these dates in an array. These dates are stored as strings. Is there a way I can sort these Date Strings in ascending or descending order?

If yes, how can I do this? If not, is there something that I could do, instead of what I am doing now? (of storing dates as strings etc.,)

--Wisdom Seeker, Andy

Replies are listed 'Best First'.
Re: Sorting Array of dates in PERL
by hardburn (Abbot) on Jun 28, 2004 at 20:42 UTC

    The DateTime module overloads the string/numeric comparison operators ('<=>' and 'cmp'), so if you can get them into DateTime objects instead of strings, you can use a trivial sort operation (my @sorted_dates = sort { $a <=> $b } @unsorted_dates;.

    Otherwise, if you store them in DDMMYYYY format, you can do the same thing (or use 'cmp' if you have slashes in the date). Personally, I prefer to use DateTime since I don't want to be constrained by a specific format.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      don't you mean YYYYMMDD format? I don't think DDMMYYYY would sort properly.

        Ahh, yes, you're right.

        And getting details like this wrong is exactly why you should just use DateTime :)

        ----
        send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Sorting Array of dates in Perl
by Happy-the-monk (Canon) on Jun 28, 2004 at 20:42 UTC

    Is there a way I can sort these Date Strings in ascending or descending order?

    Sure, see   perldoc -f sort.
    sort can sort numerical strings in ascending:   { $a <=> $b }   and descending   { $b <=> $a }   order.

    You may find a multitude of Time and Date modules on CPAN if you feel strings aren't handy enough.

    Cheers, Sören

Re: Sorting Array of dates in PERL
by jZed (Prior) on Jun 28, 2004 at 20:48 UTC
    These dates are stored as strings
    What kinds of strings? "January 15, 1953" or "01/015/53" or "15/01/1953" or ...?

    If the dates are stored as YYYYMMDD (e.g 1953-01-15 or 19530115 or 1953/01/15), then a simple perl sort of the array will put them in chronological order. If they aren't in that format, then you'll probably need to either munge them into that format or use a module like Date::Manip or Date::Calc.

Re: Sorting Array of dates in PERL
by gri6507 (Deacon) on Jun 28, 2004 at 20:45 UTC
Re: Sorting Array of dates in PERL
by rupesh (Hermit) on Jun 29, 2004 at 12:21 UTC

    Im assuming your date is in the form mm-dd-yy.

    @datesort can contain any text embedded with the date. The below example considers that the line starts of with the digits of the date. You can however modify the regex based on your code.
    @datesort = qw(6-1-04 5-4-04 14-21-04 12-3-04); my @data = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { my($m,$d,$y) = /^(\d+)-(\d+)-(\d+)/; [ $_, sprintf "20%d%02d%02d", $y, $m, $d ] } (@datesort); foreach (@data) { print "$_\n"; } ^D 5-4-04 6-1-04 12-3-04 14-21-04