Perl Language Sorting Case Insensitive Sort


Example

The traditional technique to make sort ignore case is to pass strings to lc or uc for comparison:

@sorted = sort { lc($a) cmp lc($b) } @list;

This works on all versions of Perl 5 and is completely sufficient for English; it doesn't matter whether you use uc or lc. However, it presents a problem for languages like Greek or Turkish where there is no 1:1 correspondence between upper- and lowercase letters so you get different results depending on whether you use uc or lc. Therefore, Perl 5.16 and higher have a case folding function called fc that avoids this problem, so modern multi-lingual sorting should use this:

@sorted = sort { fc($a) cmp fc($b) } @list;