- or download this
@sorted = sort { $a <=> $b } @numbers; # ascending order
@sorted = sort { $b <=> $a } @numbers; # descending order
- or download this
@sorted = sort { $a cmp $b } @unsorted;
- or download this
@sorted = sort @unsorted;
- or download this
@sorted = sort { lc($a) cmp lc($b) } @unsorted;
# or
@sorted = sort { uc($a) cmp uc($b) } @unsorted;
- or download this
sub age_or_name {
my ($name_a, $age_a) = split /_/ => $a;
...
@sorted = sort age_or_name @people;
# @sorted is now
# qw( Jon_14 Tim_14 Ray_18 Greg_19 Jeff_19 Joan_20 )
- or download this
@sorted = sort { ... } @strings;
- or download this
@nodes = (
{ id => 17, size => 300, keys => 2, cmp => 'keys' },
...
{ id => 31, size => 2045, keys => 43, cmp => 'keys' },
{ id => 28, size => 6, keys => 0, cmp => 'id' },
);
- or download this
{
my %cache; # cache hash is only seen by this function
...
@people = qw( Jeff_19 Jon_14 Ray_18 Tim_14 Joan_20 Greg_19 );
@sorted = sort age_or_name @people;
- or download this
{
my %cache;
...
# compare as needed
}
}
- or download this
# sorts in-place (meaning @list gets changed)
# set $unknown to true to indicate variable length
radix_sort(\@list, $unknown);
- or download this
sub radix_sort {
my ($data, $k) = @_;
...
@$data = map @$_, @buckets; # expand array refs
}
}
- or download this
sub radix_sort (\@;$);
...
sub radix_sort (\@;$) {
# ...
}
- or download this
@names = qw( Jeff Jon Ray Tim Joan Greg );
@ages = qw( 19 14 18 14 20 19 );
@gender = qw( m m m m f m );
- or download this
@names = qw( Jon Tim Ray Greg Jeff Joan );
@ages = qw( 14 14 18 19 19 20 );
@gender = qw( m m m m m f );
- or download this
sub age_or_name {
return (
...
$names[$a] cmp $names[$b]
)
}
- or download this
@idx = sort age_or_name 0 .. $#ages;
print "@ages\n"; # 19 14 18 14 20 19
print "@idx\n"; # 1 3 2 5 0 4
print "@ages[@idx]\n"; # 14 14 18 19 19 20
- or download this
@sorted =
map { get_original_data($_) }
sort { ... }
map { transform_data($_) }
@original;
- or download this
username:password:shell:name:dir
- or download this
@sorted =
map { $_->[0] }
...
}
map { [ $_, split /:/ ] }
@entries;
- or download this
@transformed = map { [ $_, split /:/ ] } @entries;
- or download this
for (@entries) {
push @transformed, [ $_, split /:/ ];
}
- or download this
@transformed = sort {
$a->[3] cmp $b->[3]
...
or
$a->[1] cmp $b->[1]
} @transformed;
- or download this
@sorted = map { $_->[0] } @transformed;
- or download this
@sorted =
map { restore($_) }
sort
map { normalize($_) }
@original;
- or download this
my $nulls = 0;
...
$nulls = length($1) if length($1) > $nulls;
}
}
- or download this
$NUL = "\0" x ++$nulls;
- or download this
# "\L...\E" is like lc(...)
@normalized = map { "\L$_\E$NUL$_" } @original;
- or download this
@sorted = sort @normalized;
- or download this
@sorted = map { (split /$NUL/)[1] } @original;
- or download this
# implement our for loop from above
# as a function
...
sort
map { "\L$_\E$NUL$_" }
@original;
- or download this
# see Exercise 1 for this function
$maxlen = maxlen(\@original);
- or download this
@sorted =
map { substr($_, $maxlen) }
sort
map { lc($_) . ("\0" x ($maxlen - length)) . $_ }
@original;
- or download this
@sorted =
{
...
or
$a->[1] cmp $b->[1]
}
- or download this
#!/usr/bin/perl -w
- or download this
package Sorting;
- or download this
sub passwd_cmp {
$a->[3] cmp $b->[3]
...
or
$a->[1] cmp $b->[1]
}
- or download this
sub case_insensitive_cmp {
lc($a) cmp lc($b)
}
- or download this
package main;
- or download this
@strings = sort Sorting::case_insensitive_cmp
qw( this Mine yours Those THESE nevER );
- or download this
print "<@strings>\n";
- or download this
__END__
<this Mine yours Those THESE nevER>
- or download this
#!/usr/bin/perl -w
- or download this
package Sorting;
- or download this
sub passwd_cmp ($$) {
local ($a, $b) = @_;
...
or
$a->[1] cmp $b->[1]
}
- or download this
sub case_insensitive_cmp ($$) {
local ($a, $b) = @_;
lc($a) cmp lc($b)
}
- or download this
package main;
- or download this
@strings = sort Sorting::case_insensitive_cmp
qw( this Mine yours Those THESE nevER );
- or download this
print "<@strings>\n";
- or download this
__END__
<Mine nevER THESE this Those yours>