Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

How do I sort a hash by its values?

by George_Sherston (Vicar)
on Jan 13, 2002 at 02:32 UTC ( #138338=categorized question: print w/replies, xml ) Need Help??
Contributed by George_Sherston on Jan 13, 2002 at 02:32 UTC
Q&A  > hashes

Answer: How do I sort a hash by its values?
contributed by tye

Don't make another structure. Just keep both the array and the hash handy. If you want all of the values (sorted), you simply do:     my @values= @hash[@array];
So, if you want the 3rd key and value, you use:

my $key= @array[2]; my $value= $hash{$key};
instead of, using your data structure:
my $key= $array_of_hashes[2]{key}; my $value= $array_of_hashes[2]{value};
or     my( $key, $value )= @{$array_of_hashes[2]}{qw( key value )};
The original array and hash also store the data using quite a bit less memory.
Answer: How do I sort a hash by its values?
contributed by particle

you can read about this in perlman:perlfaq4. additionally, you can search here for 'sort hash values' or something similar. a little legwork goes a long way.


Answer: How do I sort a hash by its values?
contributed by George_Sherston

Warning: I'm the person who asked the question!

How I'd do it is

my %hash = ( foo => 2, bar => 1, baz => 3, bun => 2, ); my @array = sort {$hash{$a} <=> $hash{$b}} keys %hash;
Of course, this array only has the hash keys in it, albeit correctly sorted. If you want a data structure with both keys *and* values from %hash then you have to choose a data structure that meets your needs. A straight hash is no good, because you have duplicate values, so you'll lose some key / value pairs when you reverse them. An array of hashes is one choice:
my @array_of_hashes; push @array_of_hashes, {key => $_, value => $hash{$_}} for @sorted;
And to see what that looks like,
use Data::Dumper; print Dumper(\@array_of_hashes);
Gets you
$VAR1 = [ { 'value' => 1, 'key' => 'bar' }, { 'value' => 2, 'key' => 'bun' }, { 'value' => 2, 'key' => 'foo' }, { 'value' => 3, 'key' => 'baz' } ];
... but I'm interested to see what other monks come up with: it was my slight unease with this solution that prompted me to post this question.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2020-02-29 03:41 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (128 votes). Check out past polls.