Why is sort changing the order of lines with identical sort keys

sort

Here is the data:

D 2
B 2
A 2

When I run this command:

sort -k2,2 file

it outputs:

A 2
B 2
D 2

My question is that when I specify only the second column -k2,2,
why is it that it sorts by the first column as well? Since all of the values of the second column are the same, it should leave it as it is.

Best Answer

That's the last resort comparison. When comparing two lines, if all the keys compare equal, then as a last resort, a basic string comparison of the whole lines is performed (-r still applies but not the other options). That behavior is specified by POSIX:

Except when the -u option is specified, lines that otherwise compare equal shall be ordered as if none of the options -d, -f, -i, -n, or -k were present (but with -r still in effect, if it was specified) and with all bytes in the lines significant to the comparison. The order in which lines that still compare equal are written is unspecified.

With GNU sort, that last-resort comparison can be disabled with the -s /--stable¹ option.


¹ as the sorting algorithm used by GNU sort is otherwise stable, so disabling that last-resort comparison results in a stable sort..

Related Question