Perl Monk, Perl Meditation | |
PerlMonks |
Re: help sorting hash of hashes by valueby graff (Chancellor) |
on Apr 07, 2022 at 01:10 UTC ( [id://11142759]=note: print w/replies, xml ) | Need Help?? |
1. You can fix it by creating a separate hash of arrays, with the sortable values as keys, and the 2-layer (original) key combinations as values.
2. As originally posted, your code is first looping over the "foo" keys (in whatever random order perl is using to return that set of hash keys, which differs from one run to the next), and then only the "ba" keys are being sorted according to the values tied to each key. Other monks can probably provide a more elegant solution, but here's the first thing that comes to mind, sticking as close as possible to the OP code: The output I get from that is consistently: UPDATE: OOPS!! I forgot that you wanted the output sorted in descending order -- so for that, you just need to change {$a<=>$b} to be {$b<=>$a} in the second outer for loop. ANOTHER UPDATE: (Silly me...) I realized that I was sticking too close to the OP code... no sorting is required in the first (nested) for loop -- here's the code again, without the unnecessary sort, and with sort direction corrected:
In Section
Seekers of Perl Wisdom
|
|