Monday, January 3, 2011

Sorting of an array in Perl

#!/usr/bin/perl

print "content-type: text/html \n\n";

@arr=qw (a c e f d h g i b j);

@arr2=sort(@arr); # default sorting in ascending order
print "Ascending: @arr2\n";

@arr2=reverse sort(@arr); # sorting in descending order
print "Descending: @arr2\n";

Output:
-------
Ascending: a b c d e f g h i j
Descending: j i h g f e d c b a