sort upper case just before lowercase key values from a hash

后端 未结 4 1571
情书的邮戳
情书的邮戳 2020-12-07 03:36

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

4条回答
  •  时光取名叫无心
    2020-12-07 03:54

    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.

提交回复
热议问题