Subject: sort(1) behavior?
To: None <current-users@NetBSD.ORG>
From: MLH <mlh@goathill.org>
List: current-users
Date: 04/08/2004 16:54:43
There is seen a high degree of variability in results with sort(1)
on various systems, particularly when using the -k field specifications.
In particular I am wondering what -b blank space handling means
and with the ordinal value of space when sorting.  Note that Solaris9
sort appears to behave the same as Gnu sort, yet Solaris8 sort
appears to behave correctly except when specifying -b for a starting
field position.

Does anyone have a good angle on how sort is supposed to work?

testsort :
---------

      0.0
      1.0
  0.0
  0.0 0.0
  0.0 1.0
  1.0
  1.0 0.0
  1.0 1.0
---------

Results:
NetBSD    | OS/X      | Solaris8  | Gnu sort
$ (g)sort -t: -k1.6nr,1.9nr -k1.1nr,1.5nr testsort
      1.0         1.0     1.0 1.0     1.0 1.0
  1.0 1.0     1.0         0.0 1.0         1.0
  0.0 1.0     1.0 0.0         1.0     0.0 1.0
      0.0     1.0 1.0     1.0 0.0     1.0    
  1.0 0.0                 0.0 0.0     1.0 0.0
  0.0 0.0         0.0         0.0                
              0.0         1.0             0.0
  1.0         0.0 0.0     0.0         0.0    
  0.0         0.0 1.0                 0.0 0.0

N - sp > 1 for col 1, < 0 for col 2
O - incorrect column sort order
S - sp < 0, (correct?)
G - 0 > sp > 1

$ (g)sort -bt: -k1.6nr,1.9nr -k1.1nr,1.5nr testsort
                  1.0     1.0 1.0     1.0 1.0
      1.0     1.0         0.0 1.0         1.0
  1.0         1.0 0.0         1.0     0.0 1.0
  1.0 0.0     1.0 1.0     1.0 0.0     1.0
  1.0 1.0                 0.0 0.0     1.0 0.0
      0.0         0.0         0.0                
  0.0         0.0         1.0             0.0
  0.0 0.0     0.0 0.0     0.0         0.0
  0.0 1.0     0.0 1.0                 0.0 0.0

N - incorrect column sort order
O - incorrect column sort order
S - sp < 0, (correct?)
G - 0 > sp > 1

$ (g)sort -t: -k1.6nr,1.9nr -k1.1bnr,1.5nr testsort
      1.0         1.0         1.0         1.0
  1.0 1.0     1.0         1.0 1.0     1.0 1.0
  0.0 1.0     1.0 0.0     0.0 1.0     0.0 1.0
      0.0     1.0 1.0     1.0 0.0     1.0
  1.0 0.0                     0.0     1.0 0.0
  0.0 0.0         0.0     0.0 0.0                
              0.0         1.0             0.0
  1.0         0.0 0.0     0.0         0.0
  0.0         0.0 1.0                 0.0 0.0

N - sp > 1 for col 1, < 0 for col 2
O - incorrect column sort order
S - col 1 sp handling suspect?
G - 0 > sp > 1 ?

$ (g)sort -t: -k1.6nr,1.9nr -k1.1nr,1.5bnr testsort
      1.0         1.0     1.0 1.0     1.0 1.0
  1.0 1.0     1.0         0.0 1.0         1.0
  0.0 1.0     1.0 0.0         1.0     0.0 1.0
  1.0 0.0     1.0 1.0     1.0 0.0     1.0
      0.0                 0.0 0.0     1.0 0.0
  0.0 0.0         0.0         0.0                
              0.0         1.0             0.0
  1.0         0.0 0.0     0.0         0.0
  0.0         0.0 1.0                 0.0 0.0

N - col 1 ?, sp < 0 for col 2
O - incorrect column sort order
S - sp < 0, (correct?)
G - 0 > sp > 1

$ (g)sort -t: -k1.6bnr,1.9nr -k1.1nr,1.5nr testsort
                  1.0     1.0 0.0     1.0
      0.0     1.0         1.0 1.0     1.0 0.0
      1.0     1.0 0.0     0.0 0.0     1.0 1.0
  1.0         1.0 1.0     0.0 1.0                
  1.0 0.0                 1.0             0.0
  1.0 1.0         0.0     0.0             1.0
  0.0         0.0                     0.0
  0.0 0.0     0.0 0.0         0.0     0.0 0.0
  0.0 1.0     0.0 1.0         1.0     0.0 1.0

N - incorrect column sort order, sp > 1
O - incorrect column sort order
S - incorrect column sort order, sp < 0
G - 0 > sp > 1

$ (g)sort -t: -k1.6nr,1.9bnr -k1.1nr,1.5nr testsort
      1.0         1.0     1.0 1.0     1.0 1.0
  1.0 1.0     1.0         0.0 1.0         1.0
  0.0 1.0     1.0 0.0         1.0     0.0 1.0
      0.0     1.0 1.0     1.0 0.0     1.0
  1.0 0.0                 0.0 0.0     1.0 0.0
  0.0 0.0         0.0         0.0                
              0.0         1.0             0.0
  1.0         0.0 0.0     0.0         0.0
  0.0         0.0 1.0                 0.0 0.0

N - sp > 1
O - incorrect column sort order
S - sp < 0 (correct?)
G - 0 > sp > 1

------------
$ cat testsort2
 1.000 1.000
       1.000
 1.000 1.000
       1.000
 0.000 1.000
       1.000
 1.000 1.000
 0.000 1.000
       1.000

Results:
  NetBSD    |     OS/X      |     Solaris8  |    Gnu sort
$ (g)sort -t : -k1.7nr,1.12nr -k1.1nr,1.6nr testsort2
       1.000     1.000 1.000     1.000 1.000     1.000 1.000
       1.000     1.000 1.000     1.000 1.000     1.000 1.000
       1.000     1.000 1.000     1.000 1.000     1.000 1.000
       1.000     0.000 1.000     0.000 1.000           1.000
 1.000 1.000     0.000 1.000     0.000 1.000           1.000
 1.000 1.000           1.000           1.000           1.000
 1.000 1.000           1.000           1.000           1.000
 0.000 1.000           1.000           1.000     0.000 1.000
 0.000 1.000           1.000           1.000     0.000 1.000

(NetBSD-current sort was used)