Wednesday, April 13, 2011

sorted by length and alphabetical order

#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";

No comments: