Sorting is an O(n log n) operation. Testing a list for sortedness is an O(n) operation. Test first, then sort if necessary.
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;
}
Strategies for testing whether a change has been made after sorting has been applied will either involve a checksum of some type (ie, take an MD5 of the list's contents before and again after), or keeping a copy of the pre-sorted list for comparison. One way you're throwing CPU cycles at the problem, and the other way, you're throwing memory at it. If you can just check whether sorting is necessary ahead of time, you conserve both.