users is a hash, and we know this because of the brackets following $users: $users{$value}->{count}, as demonstrated in the original question.

Based on what the OP told us, the structure could minimally look something like this:

%users = ( john => { count => 1 }, frank => { count => 3 } );

If the goal is to sort the list of users based on the value of count, you're not really doing that. You're jumping to the assumption that the datastructure looks more like this:

@users = [ { count => 1 }, { count => 3 } ];

In other words, your solution assumes that @users is an array of hashrefs, as opposed to being a hash at its top level. I don't see how you're deriving that assumption based on the original question.

What your solution accomplishes is to sort the array, @users, based on $users->{count}, not based on $users{$value}->{count}.

Another perplexing topic covered in your reply is that of using '>' as the comparison operator within a sort routine. sort expects a comparison that returns either -1, 0, or 1. cmp does this, as does <=>. But there is nothing that will cause the '>' (greater than) operator to return a -1. So you're correct that "this won't work", but it has nothing to do with your choice of $::a and $::b versus $a and $b, and everything to do with an inappropriate use of the '>' operator. sort always uses a package global version of $a and $b anyway.


Dave


In reply to Re^2: Sorting data structure by davido
in thread Sorting data structure by mhearse

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.