Dear Monks, I have a Hash of Hashes, in which the first level contains the names of nodes and the second level contains values for their 'cores' and 'memory', it looks like this:
$free_space = { "node1" => { "cores" => 12, "mem" => 200 }, "node2" => { "cores" => 12, "mem" => 500 }, ... }
What I'd like to do is print it out while sorting by 'cores' (descendingly) first, 'memory' (descendingly) second and nodenames (ascendingly) last. So sort by cores, and if the number of cores is equal then sort by memory, if both are equal then sort lexicographically by the node names. It's supposed to look like this:
cores memory node ----- ------ ---- 12 400 node456 12 400 node534 12 350 node23 11 500 node12 11 200 node3 10 900 node10
I've tried doing it "manually" by creating a new HoHoA, where the 'cores' values are first-level keys, 'memory' values are second-level keys and the array holds the names of the nodes, then printing it simply by sorting level by level, like this:
my %CoreMemNodes = (); #HoA <free_cores> -> <free_mem> -> (node1,node2 +,..) foreach my $node (keys %free_space) { push @{ $CoreMemNodes{$free_space{$node}{'cores'}}{$free_space{$no +de}{'mem'}} }, $node; } foreach my $free_cores (sort { $b <=> $a } keys %CoreMemNodes) { foreach my $free_mem (sort { $b <=> $a } keys %{ $CoreMemNodes{$fr +ee_cores} }) { foreach my $node (sort @{ $CoreMemNodes{$free_cores}{$free_mem +} }) { print "SORTED:$free_cores $free_mem $node\n"; } } }
Is there a more efficient solution?

In reply to sort HoH by second level values in specific order by lener

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.