sort upper case just before lowercase key values from a hash

后端 未结 4 1576
情书的邮戳
情书的邮戳 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:48

    Use a custom sort which first compares the items based on their lowercased representations (so that all variations of "jane" appear before variations of "jim"), then resolves ties by doing a default ASCII comparison (where uppercase comes before lowercase):

    perl -e 'print join "\n", sort { lc $a cmp lc $b || $a cmp $b } qw( jim JANE jane JIM )'
    

    Output:

    JANE
    jane
    JIM
    jim
    

提交回复
热议问题