Reputation: 619
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
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