how to sort hash by value, where value is a valid date

P

perl_help_needed

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
.........etc..
here value can be any valid date in the format yy/mm/dd.
Could anyone help me out with this one please!!
Thanks..
 
B

Brian Wakem

perl_help_needed said:
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
........etc..
here value can be any valid date in the format yy/mm/dd.
Could anyone help me out with this one please!!
Thanks..


You cannot actually sort a hash, but your can access its contents in
specified order in a foreach loop, like this:-


foreach (sort { $hash{$a} cmp $hash{$b} } keys %hash) {
print "Date:$hash{$_}\tKey:$_\n";
}
 
J

J. Gleixner

perl_help_needed said:
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
........etc..
here value can be any valid date in the format yy/mm/dd.
Could anyone help me out with this one please!!
Thanks..

What have you tried?

Due to the format, using 'sort' is fine.

my %h = ( 48487 => '05/05/18', 52327 => '05/05/23', 123 => '04/01/01');
print 'Sorted=', join(',', sort values %h), "\n";
Sorted=04/01/01,05/05/18,05/05/23

perldoc -f values
perldoc -q "How do I sort a hash"
 
T

Tad McClellan

perl_help_needed said:
I want to sort a hash by its vlaue in perl.


perldoc -q sort

How do I sort a hash (optionally by value instead of key)?

Could anyone help me out with this one please!!


You are expected to check the Perl FAQ *before* posting to
the Perl newgroup.
 
M

mothra

perl_help_needed said:
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
 
G

greymaus

You cannot actually sort a hash, but your can access its contents in
specified order in a foreach loop, like this:-


foreach (sort { $hash{$a} cmp $hash{$b} } keys %hash) {
print "Date:$hash{$_}\tKey:$_\n";
}

Dosn't this flatten the hash if values are similiar?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top