I'm looking for a nice way to sort a hash in Perl by value first and by key afterwards.
Example:
my %userids = (
williams => "Marketing",
smith => "Research",
johnson => "Research",
jones => "Marketing",
brown => "Marketing",
davis => "Research"
);
Output:
Marketing: brown
Marketing: jones
Marketing: williams
Research: davis
Research: johnson
Research: smith
Please note that value was the first sorting level. Second sorting level is key. Any idea how to do this in an elegant and high-performance way? Thanks!
I would like to add one more thing, use
\L sequence
in sorting.From Perlfaq4: How do I sort a hash (optionally by value instead of key)?
To make our report order case-insensitive, we use the
\L sequence
in a double-quoted string to make everything lowercase. Thesort()
block then compares the lowercased values to determine in which order to put the keys.Output :
2.
Output:
Good reference: http://www.misc-perl-info.com/perl-sort.html#shv