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

I'm new to PERL and I want to sort a hash by it's values and then by keys prior to printing out both. I've figured out how to do either but I'm not certain how to combine. Thanks!

Replies are listed 'Best First'.
Re: sorting hashes
by FunkyMonk (Bishop) on Oct 17, 2007 at 18:47 UTC
    use (primary test) || (secondary test) in your sort block:
    my %hash = ( A => 91, B => 82, C => 1, D=> 44, E => 82 ); my @by_value = sort { $hash{$a} <=> $hash{$b} || $a cmp $b } keys %hash; print "$_ => $hash{$_}\n" for @by_value;

      great! Thanks