Bug 10671

Summary: sort -s is not stable
Product: Busybox Reporter: 9qq3ip+an9ogcgv8nr9g
Component: OtherAssignee: unassigned
Status: RESOLVED FIXED    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Host: Target:
Build:

Description 9qq3ip+an9ogcgv8nr9g 2018-01-23 20:23:07 UTC
Someone on the Alpine Linux users mailing list observed that "sort -s" is not stable as intended.
  "
   The following two commands (with and without -s) give different
   result on alpine linux 3.6.2/amd64,
   with busybox v1.26.2 (2017-08-03 13:08:12 GMT):

     $ printf "a X 1\nA X 2\nA x 5\n" | sort -k1,1
     A X 2
     A x 5
     a X 1

     $ printf "a X 1\nA X 2\nA x 5\n" | sort -k1,1 -s
     A x 5
     A X 2
     a X 1
  "

Another user observed that sort.c uses qsort(), which is documented as not stable, and suggested adding the line number as an additional (internal) key to make it so.

Here are the mailing list posts:
  https://lists.alpinelinux.org/alpine-user/0178.html
  https://lists.alpinelinux.org/alpine-user/0180.html
Comment 1 Denys Vlasenko 2018-02-21 19:12:10 UTC
commit 7d285c78a35b1e745f7c6f27e31d73677ad2943a
Author: Denys Vlasenko <vda.linux@googlemail.com>
Date:   Wed Feb 21 20:08:54 2018 +0100

    sort: fix -s. Closes 10671