So, I am confused by this sort routine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

So, I am confused by this sort routine

#1 Post by ghostmachine4 » 05 Apr 2011 21:01

Source :Sorting text with numbers - Sorting that handles numbers as numbers not text

So , the little help text says

Code: Select all

...
test [n]

  n     Specifies the character number, n, to
        begin each comparison.  3 indicates that
        each comparison should begin at the 3rd
        character in each line.  Lines with fewer
        than n characters collate before other lines.
        By default comparisons start at the first
        character in each line.


It says "n" specifies the position of character to start sorting. So let's say we have this in a file

Code: Select all

abc10def3
abc900def4
abc29def3
ab2234defa
ab2234defv


without specifying the "n" character, it gives supposedly correct results

Code: Select all

C:\work>type file | test.bat
ab2234defa  <== since "2" is at 3rd position
ab2234defv  <=== since "v" is greater than "a" at the last position
abc10def3
abc29def3  <=== the rest are at 4th position
abc900def4


However, when tested with n = 4 for example

Code: Select all

C:\work>type file | test.bat 4
abc10def3
abc900def4 <=== why is 900 here ie, greater than "29" below?
abc29def3
ab2234defa
ab2234defv


this gives incorrect results, since starting from 4th position, these should be the correct result

Code: Select all

abc10def3 <== starting from 4th position "10" is smallest
abc29def3 <=== then 29
ab2234defa  <== then 234
ab2234defv  <== then 234 and last character "v" is greater than previous "a"
abc900def4  <== 900 is the highest.


Some one please clear up my confusion.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: So, I am confused by this sort routine

#2 Post by DosItHelp » 05 Apr 2011 21:52

"Specifies the character number, n, to begin each comparison." means that n=4 indicates that each comparison should begin at the 4th character in each line.
Adding a space after the 4th character should clarify:

Code: Select all

abc1  0def3      <== 0
abc9  00def4     <== 0 results from 00, 4 is greater than 3
abc2  9def3      <== 9
ab22  34defa     <== 34
ab22  34defv     <== 34, v is greater that a

Makes sense?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: So, I am confused by this sort routine

#3 Post by ghostmachine4 » 05 Apr 2011 22:03

DosItHelp wrote:"Specifies the character number, n, to begin each comparison." means that n=4 indicates that each comparison should begin at the 4th character in each line.
Adding a space after the 4th character should clarify:

Code: Select all

abc1  0def3      <== 0
abc9  00def4     <== 0 results from 00, 4 is greater than 3
abc2  9def3      <== 9
ab22  34defa     <== 34
ab22  34defv     <== 34, v is greater that a

Makes sense?


Well, it makes sense if the statement says "each comparison should begin after the nth character in each line." instead of "each comparison should begin at the 4th character in each line." . With "begin at" , I take it to mean literally starting from the 4th character. In your example, I view it as starting from the 5th character. I really should have specified n=3 if I want it to start at 4th character. thanks for clarifying.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: So, I am confused by this sort routine

#4 Post by DosItHelp » 05 Apr 2011 22:23

Ok, I see what you mean. This has been fixed now, character position starts with 1 now and not with 0.
Thanks a lot for pointing this out!

Post Reply