Sorting
$a and $b are special package variables in sort blocks. Don't declare them with my.
Perl's sort is a stable merge sort (since 5.8).
use v5.36;
my @words = qw(banana Apple cherry date);
my @nums = (10, 2, 33, 4);
my @alpha = sort @words; # default: string (ASCII) order
my @numeric = sort { $a <=> $b } @nums; # numeric ascending
my @desc = sort { $b <=> $a } @nums; # numeric descending
my @ci = sort { lc($a) cmp lc($b) } @words; # case-insensitive
my @by_len = sort { length($a) <=> length($b) or $a cmp $b } @words; # multi-key
my %score = (ana => 90, bo => 85, cy => 90);
my @ranked = sort { $score{$b} <=> $score{$a} || $a cmp $b } keys %score;
my @people = ({ name => 'Ana', age => 30 }, { name => 'Bo', age => 25 });
my @by_age = sort { $a->{age} <=> $b->{age} } @people;
# Schwartzian Transform: compute an expensive key once per element
my @sorted_files =
map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, -s $_] } # file size computed once
glob '*.log';
# Named sort subroutine
sub by_number { $a <=> $b }
my @s = sort by_number @nums;
use Sort::Naturally qw(nsort); # CPAN: "file2" before "file10"
use Unicode::Collate; # locale-aware/Unicode collation
my @collated = Unicode::Collate->new->sort(@wo
…