in reply to Re: Sorting by Date Help!
in thread Sorting by Date Help!

Still did not work, but another question to try to simplify what I am asking is if a had a Array of Hashes, like:

my @data; push @data, {name => "Submitted", date => 01/03/08 }; push @data, { name => "Submitted", date => 04/04/09 }; push @data, { name => "Submitted", date => 12/01/08}; push @data, { name => "Submitted", date => 12/01/09}; push @data, { name => "Submitted", date => 08/10/08}; my @sorted = sort { $a->{date} <=> $b->{date} } @data;

Would that sort by the most recent date?

Replies are listed 'Best First'.
Re^3: Sorting by Date Help!
by moritz (Cardinal) on Mar 17, 2009 at 18:56 UTC
    Still did not work

    In what way? I'm surprised... show us what code you've tried, so that we can tell you what's wrong and how to fix it.

    push @data, {name => "Submitted", date => 01/03/08 };

    If you write 01/03/08 it will complain with Illegal octal digit '8' at.

    Would that sort by the most recent date?

    No, because you still haven't fixed the comparison semantics.

      I tried like that and did not get the dates in the order of the most recent dates on top.
      my @data; push @data, {date => '01-03-08'}; push @data, {date => '04-04-09'}; push @data, {date => '12-01-08'}; push @data, {date => '12-01-09'}; push @data, {date => '08-10-08'}; my @sorted = sort { $a->{date} <=> $b->{date} } @data; foreach my $res(@sorted) { print $res->{ date }."\n\n"; }

        You need to put the year first in the date (not last; use the format YY-MM-DD, and then use cmp for comparison, not <=>. Then all that's left is reversing the order, which you'll surely figure out yourself.