Sorting in Perl:


Updated: 2017-01-23-Mon 23:48:02 UTC



Alphabetic Sorting:

back to menu

The sort() function in Perl is a one-argument function that takes as its argument - most typically - either a list or an array.

Please see the perldoc documentation for more info on sort() function here

As an example of why we might need to format data, let's think about sorting strings in Perl. Specifically, let's think about the task of sorting strings that contains digits in them. But first let's look at just a normal alphanumeric sort.

The sort() function in Perl performs an alphanumeric sort in Perl in the manner of sorting like a dictionary would be sorted. Dictionary sorting orders things alphabetically. So, "acorn" would be sorted to come before "apple", and "apple" will be sorted to come before "applesauce". This is perfect behavior for sort - it's exactly what alphabetical sorting should be!

However, now let's think about sorting numbers. In Perl, if you have an array that you wish to sort - regardless of whether the contents of the array are numerics or strings containing digits - the sort() function still performs a dictionary sort on these array indices.

If we looked at sort with the following code, we wouldn't notice any errors.

In the code above, we wouldn't notice that whether a numeric sort was successful or not. The reason is because of the data that we used - the data just happened to sort alphabetically in the same order as it would were it also a numeric sort.

We were to add some more numbers - the right numbers - to our set, we'd notice the problem. The next code, with just a few more numbers (the right numbers that will show us the nature of this sort), will show us that this sort is not numeric.

So, let's think about an example. If we have a simple array with just 2 items in it - the numbers 101 and 5, performing a sort on this array would yield a list with these two items sorted with 101 before 5. If you want a numeric sorting of the numbers that orders them from smallest to largest number (by value), this is not good. What Perl gives you is '101 comes before 5 because, in the dictionary, anything that starts with '1' - regardless of the length of the value - should come before anything that starts with '5'. Again, for numeric sorting, not so good!

The documentation for the sort function contains a fair amount of information about various ways to sort - beyond that function itself - e.g., it documents the cmp operator which does string comparisons in a manner similar to <=>.
Here is the perldoc page for sort (described next).

Furthermore, the perldoc documentation on the Perl's various operators describes them more.
Here is the perldoc page for perl operators.

back to menu

Numeric Sorting:

back to menu

The Spaceship Operator: <=>

The spaceship operator looks like this: <=>

The spaceship operator is a comparison operator - it compares operands. It is a binary comparison operator - it takes two operands. It is also a numeric comparison operator - the two operands should be either numeric values (e.g., -1, 0, 18.3, etc.) or strings containing characters that represent numeric values (e.g., "-19", '33', "-9.3" ). Like other binary numeric comparison operators, the spaceship operator compares two numeric oprands and returns a value based on comparing the two operands.

However, unlike many other binary numeric comparison operators that we have learned in Perl, the return value of the spaceship operator is NOT a boolean value.

What are the binary numeric comparison operators that return boolean values in Perl?

For each of the operators in the list above, the return value of a comparison exclusively resolves to either TRUE or FALSE.

In contrast to those boolean operators that return TRUE or FALSE values, the spaceship operator does not return one of two possible values - it returns one of three possible values.

If the two numbers being compared are equal, then the spaceship operator returns 0. If the left operand (LHS) is greater than the right operand (RHS), then the spaceship operator returns 1. If the LHS is less than the RHS, then the spaceship operator returns -1.

To get an idea of this, let's look at some examples.

WARNING: you will encounter problems if you're not careful about your data comparisons and you don't issue the pragma statement use warnings; !
Notice in the code below that 'use warnings;' is commented out.

Warning: just by adding use warnings; to your code, you will not escape this problem that you have above.
If you run that code with use warnings; uncommented, you will get a friendly warning, but you will still evaluate to and print '0'.
With warnings, you will get something like the following:

So, is this a problem with Perl?
No, Perl behaves exactly as it is was designed,
and it can be quite powerful therefrom.
You just have to be careful that you know your data well!

back to menu

Sorting Arrays with <=>

The following code sorts an array of numbers and places them into a new array in sorted order.

If you want to sort an array of numbers in descending order, you simply need to switch the order of the scalar variables $a and $b.

back to menu

Even More Sorting

The documentation for the sort function contains a fair amount of information about various ways to sort - beyond that function itself - e.g., it documents the cmp operator which does string comparisons in a manner similar to <=>.
Here is the perldoc page for sort.

Furthermore, the perldoc documentation on the Perl's various operators describes them more.
Here is the perldoc page for perl operators.

back to menu