9.3. Sort using a function

Example 9-4. examples/subroutines/sort_with_function.pl

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

my @data = (23, 1, 12, 3, 48);
my @sorted = sort by_number @data;

print "@data\n";
print "@sorted\n";

sub by_number {
    return $a <=> $b;
}

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed