Sort hash by value and key (in that order)

2019-03-19 09:47发布

问题:

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!

回答1:

Good reference: http://www.misc-perl-info.com/perl-sort.html#shv

#!/usr/bin/perl

my %userids = (
    williams => "Marketing",
    smith    => "Research",
    johnson  => "Research",
    jones    => "Marketing",
    brown    => "Marketing",
    davis    => "Research"
);

foreach (sort { ($userids{$a} cmp $userids{$b}) || ($a cmp $b) } keys %userids) 
{
    print "$_: $userids{$_}\n";
}


回答2:

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. The sort() block then compares the lowercased values to determine in which order to put the keys.

foreach (sort { $userids{$a} cmp $userids{$b} or "\L$a" cmp "\L$b" )  {
    print "$_: $userids{$_}\n"; 
} 

Output :

brown: Marketing
jones: Marketing
williams: Marketing
davis: Research
Johnson: Research # here 'J'ohnson, J is in uppercase(taking assumption), come as fifth record
smith: Research

2.

foreach (sort { $userids{$a} cmp $userids{$b} or $a cmp $b )  {
    print "$_: $userids{$_}\n"; 
}

Output:

brown: Marketing
jones: Marketing
williams: Marketing
Johnson: Research # here it shifted to fourth record
davis: Research
smith: Research