Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I sort a hash by its values?

by George_Sherston (Vicar)
on Jan 13, 2002 at 02:32 UTC ( [id://138338]=perlquestion: print w/replies, xml ) Need Help??

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

How do I sort a hash by its values?

This was orginally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I sort a hash by its values?
by tye (Sage) on Jan 13, 2002 at 04:24 UTC
    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.
Re: How do I sort a hash by its values?
by particle (Vicar) on Jan 13, 2002 at 02:46 UTC
    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.

    ~Particle

      Yeah, actually I did do the leg work and I did find out a bit about it... but the first place I looked was Q&A and it wasn't there. And I thought this was a subject that comes up quite often and wd be worth representing here. Your unhelpful (but easy) answer "a little legwork goes a long way" is an answer to *any* categorised question, no?

      § George Sherston
Re: How do I sort a hash by its values?
by George_Sherston (Vicar) on Jan 13, 2002 at 03:03 UTC
    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.
Re: How do I sort a hash by its values?
by George_Sherston (Vicar) on Jan 13, 2002 at 02:48 UTC
    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, "$hash{$_} => $_\n" for @array;
    And to see what that looks like,
    use Data::Dumper; print Dumper(\@array_of_hashes);
    Gets you
    $VAR1 = [ '1 => bar', '2 => bun', '2 => foo', '3 => 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.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://138338]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found