SharonKo
SharonKo

Reputation: 619

Case independent sorting

How do I sort hash table by key (phonetically) I mean, if there is 3 keys in the hash table (called %tags), "MWE", "wPrefix", "conjunction", if I use the regular sort:

foreach $tag (sort keys %tags) {
    print "$tag\n";
}

The output I get is:

MWE
conjunction
wPrefix

But the output should be:

conjunction
MWE
wPrefix

Upvotes: 6

Views: 1865

Answers (1)

Miguel Prz
Miguel Prz

Reputation: 13792

Use the block code for sort function, comparing the upper case of each item:

foreach $tag (sort {uc($a) cmp uc($b)} keys %tags) {
    print "$tag\n";
}

This is a case-insensitive sorting, as @Dave Sherohman points

Upvotes: 9

Related Questions