Perl Language Sorting Basic Lexical Sort


Example

@sorted = sort @list;

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

sub compare { $a cmp $b }
@sorted = sort compare @list;

The three examples above do exactly the same thing. If you don't supply any comparator function or block, sort assumes you want the list on its right sorted lexically. This is usually the form you want if you just need your data in some predictable order and don't care about linguistic correctness.

sort passes pairs of items in @list to the comparator function, which tells sort which item is larger. The cmp operator does this for strings while <=> does the same thing for numbers. The comparator is called quite often, on average n * log(n) times with n being the number of elements to be sorted, so it's important it be fast. This is the reason sort uses predefined package global variables ($a and $b) to pass the elements to be compared to the block or function, instead of proper function parameters.

If you use locale, cmp takes locale specific collation order into account, e.g. it will sort Å like A under a Danish locale but after Z under an English or German one. However, it doesn't take the more complex Unicode sorting rules into account nor does it offer any control over the order—for example phone books are often sorted differently from dictionaries. For those cases, the Unicode::Collate and particularly Unicode::Collate::Locale modules are recommended.