How to sort an array alphabetically in Perl?

Question

How to sort an array alphabetically in Perl?

How to Sort an Array Alphabetically in Perl

Sorting an array alphabetically is a common task in Perl, and the language provides a very straightforward way to do this using the sort function. By default, sort compares elements as strings in ASCII order, which is perfect for alphabetical sorting.

In Perl, arrays are denoted with the @ sigil, and when you use sort, it returns a new list sorted in ascending alphabetical order by default. You can then assign this sorted list back to your array if you want to modify it in place.

Basic Alphabetical Sorting

Here is a simple example demonstrating alphabetical sorting of an array of words:

#!/usr/bin/perl
use strict;
use warnings;

my @words = ('banana', 'apple', 'grape', 'orange', 'pear');

# Sort the array alphabetically
my @sorted = sort @words;

print "Original array: @words\n";
print "Sorted array: @sorted\n";

This code will output:

Original array: banana apple grape orange pear
Sorted array: apple banana grape orange pear

Explanation and Perl Concepts

  • sort function: In scalar context, sort returns a sorted list in ascending order based on string comparison.
  • Sigils: Variables starting with @ denote arrays. When passing arrays to functions, they flatten into lists.
  • TMTOWTDI (There's More Than One Way To Do It): Perl allows you to customize sorting by providing a block or subroutine to sort. For example, for case-insensitive sorting:
    my @sorted_ci = sort { lc($a) cmp lc($b) } @words;
    This converts elements to lowercase before comparing.
  • Context: sort operates in list context, returning a sorted list.

Common Pitfalls

  • Not assigning the output of sort back to an array or variable, leading to no change.
  • Assuming sort changes the array in place—it does not; you must reassign.
  • Sorting numbers with sort will sort them lexicographically (e.g., '10' before '2'), so use numeric comparison { $a <=> $b } for that.
  • Beware of case sensitivity: sort is case-sensitive by default, so uppercase letters sort before lowercase.

Version Notes

The basic sort behavior has been consistent in Perl for many versions (5.x). Features like the fc function for case folding (introduced in Perl 5.16) can be used for locale-aware case-insensitive sorting:

my @sorted_locale = sort { fc($a) cmp fc($b) } @words;

This is more advanced than the basic example but useful for internationalized text.

Summary

To sort an array alphabetically in Perl, simply use my @sorted = sort @array;. For more control, provide a sorting block. Always remember to assign the sorted list back if you want your original variable sorted.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 7ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Original array: banana apple grape orange pear
Sorted array: apple banana grape orange pear
STDERR
(empty)

Was this helpful?

Related Questions