/bin/sort bug

Graham Ross grahamr at bronze.UUCP
Fri Sep 2 03:19:29 AEST 1983


/bin/sort's quick sort algorithm is unstable (in the sense of Knuth, ACP,
Vol. 3, p. 4).  To achieve stability, do the following:

cat -n <input | sort ...your_flags... +0n | sed 's/^[^\t]\t//' >output

This prepends an identifying number to each line in the input, which stays
with it until the sorting is done.  The +0n key in the sort gives a numeric
comparison on this first field.  Remember to bump the column numbers in
you original sort flags.  The "\t"s in the sed command probably should be
real tabs (I forget).  If you don't have cat's -n option (another UCB
monstrosity, I think) you can easily write a separate program to do it.

	-Graham Ross
	teklabs!tekmdp!grahamr



More information about the Comp.unix.wizards mailing list