按值和键对哈希进行排序(按顺序)

发布于 2024-12-10 07:45:39 字数 498 浏览 0 评论 0原文

我正在寻找一种很好的方法来在 Perl 中先按值排序,然后再按键排序。

示例:

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

输出:

Marketing: brown
Marketing: jones
Marketing: williams
Research: davis
Research: johnson
Research: smith

请注意,value 是第一个排序级别。第二个排序级别是。知道如何以优雅且高性能的方式做到这一点吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-12-17 07:45:39

很好的参考: 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";
}

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";
}
丢了幸福的猪 2024-12-17 07:45:39

我想再添加一件事,在排序中使用 \Lequence

来自Perlfaq4:如何对哈希进行排序(可以选择按值而不是键)?

为了使我们的报告顺序不区分大小写,我们在双引号字符串中使用 \L 序列 使所有内容都小写。然后,sort() 块会比较小写值以确定键的放置顺序。

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

输出:

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"; 
}

输出:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文