############################### # try to sort fields as numbers first, then as strings # keep going until there's a difference sub smart_sort { my @a = split /[\s_]+/, lc($a); my @b = split /[\s_]+/, lc($b); for my $i ( 0..(@a < @b ? $#a : $#b) ) { if ( $a[$i] =~ $NUMBER_ONLY_REGEX # line 214 and $b[$i] =~ $NUMBER_ONLY_REGEX and $a[$i] <=> $b[$i] ) { return $a[$i] <=> $b[$i]; } elsif ( ( my @ai = /^(\d+)(.+)/ ) # error/bug is here and ( my @bi = /^(\d+)(.+)/ ) ) { if ( $ai[0] <=> $bi[0] ) { return $ai[0] <=> $bi[0]; } elsif ( $ai[1] cmp $bi[1] ) { return $ai[1] cmp $bi[1]; } } elsif( $a[$i] cmp $b[$i] ) { return $a[$i] cmp $b[$i]; } } return $a cmp $b; } #### sub smart_sort { my @a = lc($a) =~ /(\d+|[a-z]+)/ig; # better split on fields my @b = lc($b) =~ /(\d+|[a-z]+)/ig; for my $i ( 0..(@a < @b ? $#a : $#b) ) { # try numbers first if ( $a[$i] =~ $NUMBER_ONLY_REGEX and $b[$i] =~ $NUMBER_ONLY_REGEX and $a[$i] <=> $b[$i] ) { return $a[$i] <=> $b[$i]; } # fallback to string compare if( $a[$i] cmp $b[$i] ) # no "elsif" { return $a[$i] cmp $b[$i]; } } # can't decide by fields, decide by whole string return $a cmp $b; }