in reply to Re: A new golf challenge - Natural Sorting
in thread A new golf challenge - Natural Sorting
Wups - there's a bug in that version of _nsc. Here's a better one:
sub _nsc { my @a = @{$_[0]}; my @b = @{$_[1]}; @a && @b or return( @a ? 1 : @b ? -1 : 0 ); my $x = shift @a; my $y = shift @b; $x =~ /\d/ ? ( $y =~ /\d/ ? length($x)<=>length($y) || $x <=> $y || _nsc(\@a,\@b) : -1 ) : ( $y =~ /\d/ ? 1 : lc($x) cmp lc($y) || _nsc(\@a,\@b) ) }
(This version also optimizes to only compare numeric strings as numbers iff they have the same length.)
|
|---|