Re: how to sort hash by value, where value is a valid date
- From: "mothra" <mothra@xxxxxxxxxxxxxxxx>
- Date: Tue, 31 May 2005 12:54:28 -0700
perl_help_needed wrote:
> Hi all,
> I want to sort a hash by its vlaue in perl. Value is a valid date.
> example:
> key value
> 48487 05/05/18
> 52327 05/05/23
> 64998 05/05/24
> 44147 05/05/18
use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;
use Data::Dumper;
our $parser =
DateTime::Format::Strptime->new( pattern => '%y/%m/%d' );
my %date_hash=();
while (<DATA>) {
my ($key, $date) = split;
$date_hash{$key} = $parser->parse_datetime( $date );
}
foreach (sort { $date_hash{$a} cmp $date_hash{$b} } keys %date_hash) {
print "Date:$date_hash{$_}\tKey:$_\n";
}
__DATA__
48487 05/05/18
52327 05/05/23
64998 05/05/24
44147 05/05/18
Hope this helps
.
- References:
- how to sort hash by value, where value is a valid date
- From: perl_help_needed
- how to sort hash by value, where value is a valid date
- Prev by Date: Re: how to sort hash by value, where value is a valid date
- Next by Date: Re: how to test if an element belongs to an array or hash
- Previous by thread: Re: how to sort hash by value, where value is a valid date
- Index(es):
Relevant Pages
|
|