#Example of sorting strings first by length then by alphabetical order
@words = qw(This is a list of words using qw);
@sorted_words =
sort {
$value = (length($a) <=> length($b));
if($value == 0) {
return lc($a) cmp lc($b);
}
else
{
return $value;
}
} @words;
$"="\n";
print "@sorted_words";
@words = qw(This is a list of words using qw);
@sorted_words =
sort {
$value = (length($a) <=> length($b));
if($value == 0) {
return lc($a) cmp lc($b);
}
else
{
return $value;
}
} @words;
$"="\n";
print "@sorted_words";
No comments:
Post a Comment