(Reading time: 1 minute)

Perl's inbuilt sort function sorts array in ASCII character code. But to sort an array numerically we can use the space ship operator <=>.

 

 
 
 
@array = sort { $a <=> $b } @oldarray;
 
 
 

 

Here is an example:

 
 
 
@array = (5,3,2,6,8,12,15);
 
 
@sorted = sort { $a <=> $b} @array;
 
 
print @sorted;
 
 
 
 

Chillzee Tag Cloud

About Chillzee

Chillzee.com is an entrepreneurship portal.

The site provides informative topics on Organizational and Strategic needs.