1. You can fix it by creating a separate hash of arrays, with the sortable values as keys, and the 2-layer (original) key combinations as values.

2. As originally posted, your code is first looping over the "foo" keys (in whatever random order perl is using to return that set of hash keys, which differs from one run to the next), and then only the "ba" keys are being sorted according to the values tied to each key.

Other monks can probably provide a more elegant solution, but here's the first thing that comes to mind, sticking as close as possible to the OP code:

#!/usr/bin/perl -w use strict; my %hoh_test; $hoh_test{foo1}{bar} = -0.12697; $hoh_test{foo1}{baz} = -0.000398154; $hoh_test{foo2}{bar} = -4.0183e-05; $hoh_test{foo2}{baz} = 0; $hoh_test{foo3}{bar} = 9.966003977e-06; $hoh_test{foo3}{baz} = 0.0001939; # sort descending by value my %sorter; foreach my $foo (keys(%hoh_test)) { foreach my $ba (sort {$hoh_test{$foo}{$b} <=> $hoh_test{$foo}{$a}} + keys(%{$hoh_test{$foo}})) { push @{$sorter{$hoh_test{$foo}{$ba}}}, [ $foo, $ba ]; } } foreach my $val (sort {$a<=>$b} keys %sorter) { foreach my $keys ( @{$sorter{$val}} ) { print "foo: $$keys[0], ba: $$keys[1], value: $hoh_test{$$keys[ +0]}{$$keys[1]}\n"; } }
The output I get from that is consistently:
foo: foo1, ba: bar, value: -0.12697 foo: foo1, ba: baz, value: -0.000398154 foo: foo2, ba: bar, value: -4.0183e-05 foo: foo2, ba: baz, value: 0 foo: foo3, ba: bar, value: 9.966003977e-06 foo: foo3, ba: baz, value: 0.0001939
UPDATE: OOPS!! I forgot that you wanted the output sorted in descending order -- so for that, you just need to change {$a<=>$b} to be {$b<=>$a} in the second outer for loop.

ANOTHER UPDATE: (Silly me...) I realized that I was sticking too close to the OP code... no sorting is required in the first (nested) for loop -- here's the code again, without the unnecessary sort, and with sort direction corrected:

#!/usr/bin/perl -w use strict; my %hoh_test; $hoh_test{foo1}{bar} = -0.12697; $hoh_test{foo1}{baz} = -0.000398154; $hoh_test{foo2}{bar} = -4.0183e-05; $hoh_test{foo2}{baz} = 0; $hoh_test{foo3}{bar} = 9.966003977e-06; $hoh_test{foo3}{baz} = 0.0001939; # sort descending by value my %sorter; foreach my $foo (keys(%hoh_test)) { foreach my $ba (keys(%{$hoh_test{$foo}})) { push @{$sorter{$hoh_test{$foo}{$ba}}}, [ $foo, $ba ]; } } foreach my $val (sort {$b<=>$a} keys %sorter) { foreach my $keys ( @{$sorter{$val}} ) { print "foo: $$keys[0], ba: $$keys[1], value: $hoh_test{$$keys[ +0]}{$$keys[1]}\n"; } }

In reply to Re: help sorting hash of hashes by value by graff
in thread help sorting hash of hashes by value by Special_K

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.