Descent sort a hash by values

I've a simple hash:
%hash = (  "A" ,   30,
           "B" ,   4,
           "C" ,   1,
           "D",    20,
            );

and i'd like to sort the values with:

foreach $key (sort {$hash{$b} cmp $hash{$a} } keys %hash) {
 print "$key $hash{$key}\n";
}

This returns:
B 4
A 30
D 20
C 1

How do i sort the values so that i get:
A 30
D 20
B 4
C 1

thx.
LVL 1
MKraussAsked:
Who is Participating?
 
ozoCommented:
sort {$hash{$b} <=> $hash{$a}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.