I have a hash I need sorted..TWICE. I asked this in the chat box but that got confusing, so here's the problem.
I need to sort a hash by values (the values are all whole numbers) but in some cases, many of the hash keys have the same value. How can I then sort these alphabetically?
Unsorted
rock => 3
candle => 25
bug => 3
rain => 12
dust => 17
spider => 12
Using
foreach (sort {$saved_key{$b} cmp $saved_key{$a}} keys %saved_key)
candle => 25
dust => 17
spider => 12
rain => 12
rock => 3
bug => 3
Desired:
candle => 25
dust => 17
rain => 12
spider => 12
bug => 3
rock => 3
If two or more values are the same, I want to sort them alphanumerically. Any help would be much appreciated.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.