GNU utility: sort

sort

I have an issue sorting a file based on the first two columns.

The layout of the file is:

1 998688068 PizzaFan Insurance 22.47
5 072821325 Plaisio Computers 26.35
4 998688068 PizzaFan Food 27.32
5 456834578 G.Yannopoulos Medical 91.67

….
….

I used this command :
sort -n -k 1,2 "$fpath" -o "$fpath.ordered"

The sort result is:

1 473151252 Goodys Food 7.15
1 951515524 Atlantic SuperMarket 41.32
1 998688068 Atlantic SuperMarket 80.23
1 998688068 PizzaFan Food 61.72
1 998688068 PizzaFan Insurance 22.47
2 094321587 Vasilopoulos SuperMarket 6.50

….
….

I don't understand why all columns get sorted (see 3rd column & PizzaFan Insurance)

I think -k 1,2 means sort on column 1 and resolve ties with column 2, but it's like it doesn't work.

It's the same as using the:
sort -n "$fpath" -o "$fpath.ordered"

Best Answer

If you want a stable sort (relative order of the input rows is preserved in case of ties), you need to use the -s or --stable flag.

Related Question