Assuming that this is called like:
@sorted=sort{$tallies{my $a} <=> $tallies{my $b}} keys %tallies
, here's how it works.

keys %tallies returns a list of all of the keys in %tallies. sort sorts the provided list using the provided sort routine

The provided sort routine is given two items from the provided list (in this case the keys for %tallies), in $a and $b, and returns 1 if $a is greater, -1 if $b is greater, or 0 if they are equal. This particular sort routine takes the $a and $b from the list, looks them up in the %tallies hash, then compares the numeric values of the results using the spaceship operator (<=>).

By calling the sort routine repeatedly, Perl sorts the list, and sort returns the sorted list.

You can learn more about this by reading about sort and keys in perlfunc(1), and about <=> in perlop(1).


In reply to Re: question re: hash sorting by sgifford
in thread question re: hash sorting by Grygonos

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.