Sorting

Sorting

The sort function sorts a list (an array). The default is to sort alphabetically. However, you can define your own sorts to get around numbers and complex data structures.

Sort an array of strings

This gives you the following output:

Sort an array of numbers

The Perl sort function sorts by strings instead of by numbers. If you were to use:

The output you would see would be:

To sort numerically, declare your own sort block and use the flying saucer operator <=>:

The output would now be:

Note that $a and $b do not need to be declared, even with use strict on, because they are special sorting variables.

To find out more, see the tutorial Sorting in perl, or run the command:

Scroll to Top