在散列中的小写键值之前对大写字母进行排序 [英] sort upper case just before lowercase key values from a hash

查看:120
本文介绍了在散列中的小写键值之前对大写字母进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例:

$ b我有一个散列,我想根据大写字母出现在小写字母前面的键进行排序。
$ b

JANE

jane

JIM

jim

解决方案

要按顺序获取密钥,请对散列的键应用 sort 并使用自定义排序函数。

  my%hash =(JANE => 1,jane => 2,JIM => 3,jim => ; 4); 
my @sorted_keys = sort {
lc $ a cmp lc
$ b || $ a cmp
$ b}键%hash;

自定义排序功能首先比较字符串,就好像它们是相同的大小写,如果相等,则考虑大小写。


I have an hash and i want to sort based on the keys with upper case words appearing just before the lowercase words.

Example:

JANE
jane
JIM
jim

解决方案

To get the keys in order, apply sort with a custom sort function on the keys of the hash.

my %hash = ( JANE => 1, jane => 2, JIM => 3, jim => 4 );
my @sorted_keys = sort {
    lc $a cmp lc $b
        || $a cmp $b
} keys %hash;

This custom sort function compares strings first as if they were of the same case, and if equal, takes case into account.

这篇关于在散列中的小写键值之前对大写字母进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆