Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.60 How do I sort a hash (optionally by value instead of key)?

Reply
Thread Tools

FAQ 4.60 How do I sort a hash (optionally by value instead of key)?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      01-03-2011
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.60: How do I sort a hash (optionally by value instead of key)?

(contributed by brian d foy)

To sort a hash, start with the keys. In this example, we give the list
of keys to the sort function which then compares them ASCIIbetically
(which might be affected by your locale settings). The output list has
the keys in ASCIIbetical order. Once we have the keys, we can go through
them to create a report which lists the keys in ASCIIbetical order.

my @keys = sort { $a cmp $b } keys %hash;

foreach my $key ( @keys )
{
printf "%-20s %6d\n", $key, $hash{$key};
}

We could get more fancy in the "sort()" block though. Instead of
comparing the keys, we can compute a value with them and use that value
as the comparison.

For instance, to make our report order case-insensitive, we use the "\L"
sequence in a double-quoted string to make everything lowercase. The
"sort()" block then compares the lowercased values to determine in which
order to put the keys.

my @keys = sort { "\L$a" cmp "\L$b" } keys %hash;

Note: if the computation is expensive or the hash has many elements, you
may want to look at the Schwartzian Transform to cache the computation
results.

If we want to sort by the hash value instead, we use the hash key to
look it up. We still get out a list of keys, but this time they are
ordered by their value.

my @keys = sort { $hash{$a} <=> $hash{$b} } keys %hash;

From there we can get more complex. If the hash values are the same, we
can provide a secondary sort on the hash key.

my @keys = sort {
$hash{$a} <=> $hash{$b}
or
"\L$a" cmp "\L$b"
} keys %hash;



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
FAQ 4.60 How do I sort a hash (optionally by value instead of key)? PerlFAQ Server Perl Misc 0 03-14-2011 10:00 PM
how to sort hash by value, where value is a valid date perl_help_needed Perl Misc 12 06-02-2005 06:27 PM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments