Perl Scripts ·
Sorting Arrays in Perl: Numbers, Alphabetical and More
By Manu Mathew, Technical Lead Engineer · Published 28 January 2016 · Last updated 5 October 2026

Perl’s sort surprises almost everyone once: give it numbers, and 12 comes before 3. This page brings together several short notes on sorting arrays in Perl I wrote in 2016. I have rewritten them in one place, with tested examples and current advice, so you don’t have to piece the answer together from fragments.
Quick answer
Plain sort compares strings character by character, so uppercase letters come before lowercase and “12” before “3”. Sort numbers with sort { $a <=> $b }, words ignoring case with sort { fc($a) cmp fc($b) }, and reverse either by swapping $a and $b. Combine comparisons with or to break ties.
I tested each example with Perl 5.40, using use strict and use warnings.
What order does plain sort use?
Without a block, sort compares values as strings, character by character, so all uppercase letters sort before lowercase ones.
use strict;
use warnings;
my @words = qw(pear Apple banana Cherry);
my @sorted = sort @words; # string order: uppercase letters sort before lowercase
print "@sorted\n"; # Apple Cherry banana pear
How do I sort numbers correctly?
Use the numeric comparison operator <=> (the “spaceship” operator) in a sort block.
use strict;
use warnings;
my @numbers = (14, 3, 12, 2, 23);
print join(' ', sort @numbers), "\n"; # 12 14 2 23 3 (as strings)
print join(' ', sort { $a <=> $b } @numbers), "\n"; # 2 3 12 14 23 (as numbers)
print join(' ', sort { $b <=> $a } @numbers), "\n"; # 23 14 12 3 2 (largest first)
<=> returns -1, 0 or 1 depending on whether the left value is smaller, equal or larger. Swap $a and $b to sort largest first; that is clearer and faster than reverse sort { … }.
How do I sort words alphabetically, ignoring case?
Compare folded copies of each word with cmp: sort { fc($a) cmp fc($b) }.
use strict;
use warnings;
use feature 'fc';
my @words = qw(pear Apple banana Cherry);
my @sorted = sort { fc($a) cmp fc($b) } @words; # ignore case
print "@sorted\n"; # Apple banana Cherry pear
fc (Perl 5.16 and later) is the Unicode-aware version of lc; lc works too for plain English text. For language-aware ordering, such as accented letters, use Unicode::Collate, which ships with Perl.
How do I sort hash keys by their values?
Sort the keys with a block that compares the values, and add a second comparison with or to break ties.
use strict;
use warnings;
my %score = (Asha => 82, Ravi => 91, Joe => 82);
# Highest score first; names in alphabetical order when scores tie
for my $name (sort { $score{$b} <=> $score{$a} or $a cmp $b } keys %score) {
print "$name $score{$name}\n";
}
# Ravi 91
# Asha 82
# Joe 82
Because <=> returns 0 for a tie, or moves on to the name comparison, so the order is stable and predictable.
Need help? If your Perl sorting logic is part of an older application that needs fixing or updating, I can take a look. See Perl development, or email manu@manu.co.in.
Frequently asked questions
How do I sort an array in place?
Assign the result back to the same array: @numbers = sort { $a <=> $b } @numbers;. sort never changes the array you give it, so without the assignment the original order stays as it was.
Is Perl’s sort stable?
Yes. Perl’s default sort has been stable since Perl 5.8, so equal elements keep their original order. use sort 'stable'; states that intention in the code. A tie-breaking comparison with or, as in the hash example above, makes the order explicit for anyone reading it.
How do I sort by more than one field?
Chain comparisons with or: sort { $a->{city} cmp $b->{city} or $a->{age} <=> $b->{age} } @people sorts by city, then by age within each city. Each later comparison only runs when the earlier ones are equal.
How do I sort quickly when each sort key is slow to work out?
Use the Schwartzian transform: work out each key once with map, sort on the stored keys, then map back to the original values. This avoids recalculating an expensive key, such as a file size or a parsed date, on every comparison.
Related notes: Perl arrays: length, add, remove and search; Perl hashes; multidimensional arrays in Perl; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
An earlier version of these sorting notes followed the examples in Gabor Szabo’s Sorting arrays in Perl on Perl Maven. This version is rewritten in my own words, with new examples.
Sources: perlfunc: sort; perlop: equality operators (<=> and cmp); perlfunc: fc; Unicode::Collate; Perl Maven: Sorting arrays in Perl.