Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How do I sort a Hash of Hashes?

( #320255=categorized question: print w/ replies, xml ) Need Help??
Contributed by greywolf on Jan 10, 2004 at 00:13 UTC
Q&A  > sorting


I have a hash of hashes that looks like this:

my %products ( 1 => { name => "Floor Wax", wholesale => "50.00", retail => "75.00", }, 2 => { name => "Paper Towel", wholesale => "20.00", retail => "40.00", }, 3 => { name => "Hand Soap", wholesale => "30.00", retail => "65.00", }, );
I can sort and print this hash by the keys using the following:
foreach my $key (sort {$a <=> $b} keys %products) { print "<br>K|$key| N|$products{$key}{name}| W|$products{$key}{whol +esale}| R|$products{$key}{retail}|"; }
How would I sort %products by the name or wholesale of each product? I have tried many variations of this to no avail.
foreach my $key (sort {$products{retail}{$a} <=> $products{retail}{$b} + keys %products) { print "<br>K|$key| N|$products{$key}{name}| W|$products{$key}{whol +esale}| R|$products{$key}{retail}|"; }
I have read the hash of hashes section of the camel many times. Am I missing something obvious here or should I be transforming this into another data structure before printing.

mr greywolf

Answer: How do I sort a Hash of Hashes?
contributed by neuroball

You have to rewrite the code so, that it sorts using the string comparitor (cmp) and you have to dereference the Hash of Hashes with an arrow operator.

sort { $products{$a}->{"name"} cmp $products{$b}->{"name"} }
Answer: How do I sort a Hash of Hashes?
contributed by NetWallah

The most straightforward answer is as neuroball has stated:

sort { $products{$a}->{"name"} cmp $products{$b}->{"name"} }


This has some performance implications in that the hash dereference and comparison get performed many more times than necessary, as sort calls the compare routine each time it compares. This is a lot more then the number of elements to be compared.

If the number of elements is large, and you care about performance, you should look into the Schwartzian Transform algorithm, which perfoms the de-reference the minimum number of times.

Editor's Note: See, however, this discussion, which reveals that hash dereferences are not usually expensive enough to warrant the power and majesty of a Schwartzian transform...

Answer: How do I sort a Hash of Hashes?
contributed by injunjoel

Greetings all,
Just another suggestion. I would turn %products into an array and then use the Schwartzian Transform to do the sorting.
untested suggestion

my @products ( { name => "Floor Wax", wholesale => "50.00", retail => "75.00", }, { name => "Paper Towel", wholesale => "20.00", retail => "40.00", }, { name => "Hand Soap", wholesale => "30.00", retail => "65.00", } ); my @sorted_by_name = map{$_->[1]} sort {$a->[0] cmp $b->[0]} map{[$_->{name},$_]} @products my @sorted_by_wholesale = map{$_->[1]} sort {$a->[0] <=> $b->[0]} map{[$_->{wholesale},$_]} @products
Hope that helps,

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 wandering the Monastery: (6)
    As of 2015-11-25 04:11 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (670 votes), past polls