# # sorting strings: # @ascending = sort @unsorted; @ascending = sort { $a cmp $b} @unsorted; # # sorting numbers # @ascending = sort { $a <=> $b} @unsorted; @descending = sort { $b <=> $a} @unsorted; # # sort keys of a dictionary # foreach my $unit ( sort keys %dev_h) { ... } # # adc names, make sure adc2 precedes adc11 and adcX precedes vfcX # my @arr = qw( adc1 adc2 adc11 vfc1 vfc2 vfc11); @arr = sort { $a =~ /(\D+)(\d+)/; # e.g.: VFC2 my $a_pre = $1; # VFC my $a_i = $2; # 2 $b =~ /(\D+)(\d+)/; my $b_pre = $1; my $b_i = $2; # # if the prefixes differ, use them to sort ... # if( $a_pre ne $b_pre) { $a_pre cmp $b_pre; } # # ... otherwise use the numbers # else { $a_i <=> $b_i; } } @arr;