Skip Menu |
 

This queue is for tickets about the Sort-Naturally CPAN distribution.

Report information
The Basics
Id: 77701
Status: open
Priority: 0/
Queue: Sort-Naturally

People
Owner: Nobody in particular
Requestors: nigel.winterbottom [...] fluke.com
tsibley [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Non-numeric word-character substrings (of mixed sort strings) are sorted incorrectly
Date: Fri, 8 Jun 2012 02:51:49 -0700
To: "bug-Sort-Naturally [...] rt.cpan.org" <bug-Sort-Naturally [...] rt.cpan.org>
From: "Winterbottom, Nigel" <nigel.winterbottom [...] fluke.com>
Download (untitled) / with headers
text/plain 1.3k
Distribution name and version : Sort-Naturally-1.03 Perl version : This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi (with 40 registered patches, see perl -V for more detail) Operating System vendor and version : Linux Comark-Nigel 2.6.35-32-generic #67-Ubuntu SMP Mon Mar 5 19:39:49 UTC 2012 x86_64 GNU/Linux My application is sorting Electronic Circuit References (Capacitors Resistors etc. e.g. C1, C11, CR1). The bug is easily demonstrated using a modification of the documented example. ( I added "fo12" to the list ) #BUG DEMONSTRATION @them = nsort(qw( foo12a foo12z foo13a foo 14 9x fo12 foo12 fooa foolio Foolio Foo12a )); print join(' ', @them), "\n"; PRINTS: 9x 14 foo fooa foolio Foolio foo12 foo12a Foo12a foo12z foo13a fo12 I think "fo12" should come before "foo12". #FIX Looking at the code I spotted a possible typo, in that the comment does not agree with the code. Having fixed that up I get the following output from the above example: 9x 14 fo12 foo foo12 foo12a Foo12a foo12z foo13a fooa foolio Foolio I am much happier with that result. #PATCH diff Sort/Naturally-1.03.pm Sort/Naturally.pm 72c72 < $x2 = $y2 if $x2 > $y2; --- Show quoted text
> $x2 = $y2 if $x2 < $y2;
-- Regards Nigel Winterbottom - Senior Engineer - Comark Instruments DDI: +44 (0)1462 444232 <http://www.comarkltd.com> Comark House, Bury Mead Road, Hitchin, Herts, UK. SG5 1RT
Download (untitled) / with headers
text/plain 1.4k
On Fri Jun 08 02:52:07 2012, nigel.winterbottom@fluke.com wrote: Show quoted text
> Looking at the code I spotted a possible typo, in that the comment > does not agree with the code. Having fixed that up I get the following > output from the above example: > > 9x 14 fo12 foo foo12 foo12a Foo12a foo12z foo13a fooa foolio Foolio > > I am much happier with that result. > > #PATCH > diff Sort/Naturally-1.03.pm Sort/Naturally.pm > 72c72 > < $x2 = $y2 if $x2 > $y2; > ---
> > $x2 = $y2 if $x2 < $y2;
The comment you reference is: # Now make x2 the min length of the two: and the original code is indeed correctly described by the comment. $x2 is set to $y2 if and only if $x2 is greater than $y2, i.e. $y2 is less than $x2. If $x2 is already the smaller number, then it is left alone. If $x2 is the larger number, it it set to the smaller number in $y2. This makes $x2 the minimum. It's easy enough to try it out and see for yourself: $ perl -E '($x, $y) = @ARGV; $x = $y if $x > $y; say $x;' 7 5 5 $ perl -E '($x, $y) = @ARGV; $x = $y if $x > $y; say $x;' 5 7 5 Your desired sort order matches what would be output if numerics always came before alphabetics, instead of only coming before when at the start of the string. I do think there's a bug here, in that the sort order: 9x 14 foo fooa foolio Foolio foo12 foo12a Foo12a foo12z foo13a fo12 puts the shorter strings "fo12" and "foo12" after longer strings.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.