Yes! here is a short program to illustrate the idea:
#/usr/bin/perl use warnings; use strict; my %hash = (a => 4, b => 6, c => 2); my @sorted_keys = sort keys %hash; print "@sorted_keys\n"; my @sort_by_value = sort{my $A = $hash{$a}; my $B = $hash{$b}; $A <=> $B ## or cmp for strings }keys %hash; print "@sort_by_value\n"; __END__ Prints: a b c # simple key sort c a b # keys sorted by value of keys
The sort{} takes input from the right and sends the same output to the left except in a different order. What goes inside the {} are the "rules" for how to sort the input. $a and $b are special Perl variables that are assigned various values as needed to sort the input. The "return value" of the sort {} should be <0,0,>0 just like the cmp statement does for strings or the "spaceship" operator, <=> does for numbers.

In reply to Re^3: Sort Hash of hashes by Marshall
in thread Sort Hash of hashes by lobs

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.