Bug 264081 - sort -s (i.e., stable sort) does not work correctly
Summary: sort -s (i.e., stable sort) does not work correctly
Status: Closed Works As Intended
Alias: None
Product: Base System
Classification: Unclassified
Component: gnu (show other bugs)
Version: 13.1-RELEASE
Hardware: amd64 Any
: --- Affects Many People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-05-19 15:01 UTC by Kyle Kneisl
Modified: 2022-05-20 13:40 UTC (History)
1 user (show)

See Also:


Attachments
chrony "measurements.txt" log output sample (4.28 KB, text/plain)
2022-05-19 15:01 UTC, Kyle Kneisl
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kyle Kneisl 2022-05-19 15:01:24 UTC
Created attachment 234035 [details]
chrony "measurements.txt" log output sample

Interestingly, the -s option for sort(1), which is supposed to be a "stable sort", i.e., preserving the order of items in the original stream when they have equal sort keys, apparently does not work as intended.  I haven't exhausted all of the possibilities, but, in my test files over here, it SEEMS to work for -k1, when the sort key is the first line item.  However, it does not seem to work in other cases.

I have included a bit of my chronyd measurements log, and you can easily reproduce.

Note that in the attached file logsample, containing 32 lines in chronological order for chrony measurements, the following command:

cat logsample | sort -s -k4

produce equally keyed items (in fact, all items have equal keys in the 4th line item) that are now *NOT* in the same order as the original stream, so that this sort is NOT stable as expected with -s.  For example:


2022-05-19 13:52:03 192.168.1.197   N  2 111 111 1111   9  9 0.17  2.584e-04  6.596e-04  3.849e-06  2.155e-02  2.243e-03 98028536 4I D K
2022-05-19 13:43:30 192.168.1.197   N  2 111 111 1111   9  9 0.21  4.559e-04  5.857e-04  3.849e-06  2.155e-02  1.694e-03 98028536 4I D K
2022-05-19 08:32:09 192.168.1.197   N  2 111 111 1111   9  9 0.30  4.185e-05  6.944e-04  3.849e-06  2.234e-02  3.952e-03 98028536 4I D K
2022-05-19 08:40:46 192.168.1.197   N  2 111 111 1111   9  9 0.51 -4.130e-05  5.296e-04  3.849e-06  2.234e-02  4.471e-03 98028536 4I D K

But, 

cat logsample | sort -s -k1

works fine.

I did just upgrade everything to 13.1 yesterday, so I'm not sure how far back this goes, or if it's new.
Comment 1 Kyle Kneisl 2022-05-20 13:40:54 UTC
Erroneous bug report.  I misunderstood that -k, unless otherwise specified, continues past the field specified.  sort(1) -s works correctly.

Sorry.