my @arr1 = qw(1 two 3 0 4 Two 5 six 7 8 9 ten);
my @arr2 = sort {$a cmp $b} @arr1;
print "\n@arr2\n";
#!/usr/bin/perl
use strict;
use warnings;
my @strarr = qw(two Two six Six alien Coders Alien coderS);
my @sorted = sort {$a cmp $b} @strarr; # same result as of sort @strarr
my @sortedcase = sort { uc $a cmp uc $b } @strarr; #case-insensitivie
print "\n@sorted\n@sortedcase\n";
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
my @sorted_numbers = sort @numbers;
print "@sorted_numbers\n";
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
my @sorted_numbers = sort {$a <=> $b} @numbers;
print "@sorted_numbers\n";
#!/usr/bin/perl
use strict;
use warnings;
my @strings = qw(Jassi Alien Coders);
my @sorted_strings = sort @strings;
print "@sorted_strings\n";
The output would be:
#!/usr/bin/perl
use strict;
use warnings;
my @strings = qw(Jassi Alien Coders);
my @sorted_strings = sort {$b cmp $a} @strings; # or reverse sort @strings
print "@sorted_strings\n";
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3);
my @sorted_numbers = sort {$b <=> $a} @numbers; # or reverse sort {$aó $b} @numbers
print "@sorted_numbers\n";
#!/usr/bin/perl
use strict;
use warnings;
my %members = (
C => 1,
Java => 7,
Perl => 12,
Linux => 3,
Hacking => 8,
);
foreach my $language (sort keys %members) {
print $language . ": " . $members{$language} . "\n";
}
#!/usr/bin/perl
use strict;
use warnings;
my %members = (
C => 1,
Java => 7,
Perl => 12,
Linux => 3,
Hacking => 8,
);
# Using <=> instead of cmp because of the numbers
foreach my $language (sort {$members{$a} <=> $members{$b}} keys %members){
print $language . ": " . $members{$language} . "\n";
}
my @aliens = (
{ name => 'Jassi', age => 28},
{ name => 'Somnath', age => 27},
{ name => 'Ritesh', age => 24},
{ name => 'Santosh', age => 29},
{ name => 'Ranjan', age => 26},
{ name => 'Kaushik', age => 25},
);
#!/usr/bin/perl
use strict;
use warnings;
my @aliens = (
{ name => 'Jassi', age => 28},
{ name => 'Somnath', age => 27},
{ name => 'Ritesh', age => 24},
{ name => 'Santosh', age => 29},
{ name => 'Ranjan', age => 26},
{ name => 'Kaushik', age => 25},
);
foreach my $person (sort {$a->{name} cmp $b->{name}} @aliens) {
print $person->{name} . " is " . $person->{age} . "\n";
}
The output is:
#!/usr/bin/perl
use strict;
use warnings;
my @aliens = (
{ name => 'Jassi', age => 28},
{ name => 'Somnath', age => 27},
{ name => 'Ritesh', age => 24},
{ name => 'Santosh', age => 29},
{ name => 'Ranjan', age => 26},
{ name => 'Kaushik', age => 25},
);
foreach my $person (sort agecomp @aliens) {
# it just replaced {$a->{age} <=> $b->{age}} by agecomp inline function
print $person->{name} . " is " . $person->{age} . " years old\n";
}
sub agecomp {
$a->{age} <=> $b->{age};
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)