my @xx = ( 'dfb|cy|nmju', 'dfb|my|jhiho', 'aaa|zz|gggg' ); my @sorted_xx = sort sorter @xx; print Dumper \@xx; print Dumper \@sorted_xx; sub sorter { my @arr_a = split /\|/, $a; my @arr_b = split /\|/, $b; while ( @arr_a ) { my $sa = shift @arr_a; my $sb = shift @arr_b; my $comparison = ( $sa cmp $sb ); return $comparison if $comparison; } return 0; } __END__ $VAR1 = [ 'dfb|cy|nmju', 'dfb|my|jhiho', 'aaa|zz|gggg' ]; $VAR1 = [ 'aaa|zz|gggg', 'dfb|cy|nmju', 'dfb|my|jhiho' ];