use strict; use warnings; use List::Util qw( first ); my @x = qw( 1 3 2 4 5 ); if( defined first { $x[$_] > $x[$_+1] } 0 .. $#x - 1 ) { @x = sort { $a <=> $b } @x; }