http://www.perlmonks.org?node_id=3860

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

I have a hash containing error-codes from a web-server log, and a number as values. I would like to print them out sorted by the number. How do I do that?

Replies are listed 'Best First'.
Re: How do I sort a hash by its values?
by btrott (Parson) on Feb 22, 2000 at 22:38 UTC
    Read perlman:perlfaq4, "How do I sort a hash (optionally by value instead of key)?"

    But in short:

    @keys = sort { $hash{$b} <=> $hash{$a} } keys %hash;
    @keys now contains the sorted list of keys.