Is there a way to sort a hash by values and leave it in a hash?  I found postings regarding sorting by keys:

sort keys %hash;

I also spotted a posting on a way to sort a hash by values to put it in an array, but not leaving it in a hash:

@keys = sort{$hash{$b} <=> $hash{$a}} keys %hash;

And I found cwest's postings regarding his Tie::SortHash module.  From that I produced this:

tie %clients, 'Tie::SortHash', \%clients, $sortblock; my $sortblock = q(my $c = (split /\s+/, $a)[1]; my $d = (split /\s+/, $b)[1]; $c cmp $d || $clients{$a} <=> $clients{$b});

But this module still only seems to sort by keys and not values.  Maybe there's another way to use this module to get a sort by values.  If anyone knows, please let me know how to do it.  However, maybe I'm trying to make a hash act like an array--that certainly seems to be where I'm going wrong--when an array will do me just fine. Let me explain what I'm trying to do and why I'm trying to stay with a hash.

I'm trying to pull information out of a mySQL database which includes two fields:  client id's and client names.  I've pulled that off without much trouble and fed the data into a hash. From there, I want to display the data in a scrolling list in a web page using the CGI module. This is the code I used for that:

print $q->scrolling_list(-name=>'acct_num', -values=> [keys %clients], -labels=> \%clients);

Again, if I put a "sort" in front of "keys" on the second line, it will sort by the client identification number.  But I need to sort by the values since the users don't know our client id's.  I thought of swapping keys with values when creating the hash, but I need to pass the key to the next web page and the values are fraught with spaces and other undesirable characters (much like our clients).  A simple solution to my problem would be to be able to sort the hash into an array (like the second bit of code above) and then plop that into the labels field of the scrolling_list's properties.  But, I can't seem to get the scrolling_list command to take an array.  Is there a way for one to give it an array instead of a hash?

Any ideas will be appreciated.

-Thanks


In reply to More Sorted Business with Hashes by Spenser

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.