- Perl ›
- builtin functions ›
- here
sort function - sort array
Use the sort function to sort the array . Pass a code block for comparison as the first argument. To sort in ascending order, write $a before $b, and to sort in descending order, write $b before $a. Use <=> as the comparison operator if you want to compare as a number, and use cmp if you want to compare in lexicographical order. Note that there is no comma immediately after the code block.
# Sort in ascending order @sorted = sort {$a operator $b} @array; # Sort in descending order @sorted = sort {$b operator $a} @array;
Sorting example
This is an example that sorts in ascending order of numerical values. @nums becomes (2, 3, 5, 11).
# Sort by numerical value in ascending order my @nums = (5, 11, 3, 2); @nums = sort {$a <=> $b} @nums;
This is an example that sorts the numbers in descending order. @nums becomes (11, 5, 3, 2).
# Sort by numerical value in descending order my @nums = (5, 11, 3, 2); @nums = sort {$b <=> $a} @nums;
This is an example that sorts in ascending order in dictionary order. @nums will be (11, 2, 3, 5). When comparing 11 and 3 in dictionary order, 11 comes to bloom.
# Sort in ascending dictionary order my @nums = (5, 11, 3, 2); @nums = sort {$a cmp $b} @nums;
This is an example that sorts in descending order of dictionary order. @nums becomes (5, 3, 2, 11).
# Sort in descending order of dictionary my @nums = (5, 11, 3, 2); @nums = sort {$b cmp $a} @nums;
Omission of code block
If you omit the code block, it has the same meaning as if you sort in ascending dictionary order.
# The next two have the same meaning @nums = sort @nums; @nums = sort {$a cmp $b} @nums;
Sort by multiple conditions
Try to sort the strings in the order of longest, and if the lengths are the same, sort them in ascending dictionary order. Use || to specify the second condition. If the order cannot be determined in the first comparison, the next comparison will be made. @strs becomes ('aaa', 'bbb', 'aa', 'bb', 'a', 'b').
my @strs = ('aaa', 'bbb', 'b', 'a', 'aa', 'bb'); @strs = sort {length $b <=> length $a || $a cmp $b} @strs;
Hash values are sorted by key and output
Let's sort the hashes by key and output. The order of the keys is not guaranteed for the hash, so if you want to output in a fixed order, rearrange the keys and output.
my %points = (Tom => 100, Mike => 30, Mami => 90); for my $name (sort keys %points) { print "$name: $points{$name}\n"; }
Sorting array reference
Let's sort the array reference. The point is to dereference when passing it to the sort function and dereference it when receiving it.
my $nums = [5, 11, 3, 2]; @$nums = sort @$nums;
Sort the hash array
Let's sort the array of hashes. The example hash array contains information about multiple people. Let's sort this by the age of the person. (Note: We call it an array of hashes, but the exact meaning here is an array reference that contains a hash reference.)
Hash reference are assigned to $a and $b, so use $a->{age}, $b->{age} to get the age. Use this to make a comparison.
# Person data my $people = [ {name =>'Ken', age => 19}, {name =>'Taro', age => 9}, {name =>'Kaori', age => 23} ];; @$persons = sort {$a->{age} <=> $b->{age}} @$persons;
$persons is sorted in ascending order of age and the data looks like this:
[ {name =>'Taro', age => 9}, {name =>'Ken', age => 19}, {name =>'Kaori', age => 23} ];;