[git commit] sort: fix -s -r interaction: 'stable' order is not affected by -r

Denys Vlasenko vda.linux at googlemail.com
Tue Jan 4 22:53:21 UTC 2022


commit: https://git.busybox.net/busybox/commit/?id=cc7d2e21780c28608b00a4faf0fed297527bcbf4
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
compare_keys                                         818     820      +2

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/sort.c     |  4 +++-
 testsuite/sort.tests | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/coreutils/sort.c b/coreutils/sort.c
index 0cbb6f597..9ff777851 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -380,7 +380,9 @@ static int compare_keys(const void *xarg, const void *yarg)
 
 			/* If x > y, 1, else -1 */
 			retval = (x32 > y32) * 2 - 1;
-		} else
+			/* Here, -r has no effect! */
+			return retval;
+		}
 		if (!(option_mask32 & FLAG_no_tie_break)) {
 			/* fallback sort */
 			flags = option_mask32;
diff --git a/testsuite/sort.tests b/testsuite/sort.tests
index c51a8e475..5375f93de 100755
--- a/testsuite/sort.tests
+++ b/testsuite/sort.tests
@@ -175,6 +175,19 @@ testing "sort file in place" \
 111
 " ""
 
+testing "sort -sr (stable and reverse) does NOT reverse 'stable' ordering" \
+"sort -k2 -r -s input" "\
+b 2
+d 2
+a 1
+c 1
+" "\
+a 1
+b 2
+c 1
+d 2
+" ""
+
 # testing "description" "command(s)" "result" "infile" "stdin"
 
 exit $FAILCOUNT


More information about the busybox-cvs mailing list