File 1299-Fix-stability-in-sort-1.patch of Package erlang

From ef70fb039e254eb97a47a92490dea6f2f3e78148 Mon Sep 17 00:00:00 2001
From: Raimo Niskanen <raimo@erlang.org>
Date: Tue, 10 Jun 2025 13:38:28 +0200
Subject: [PATCH 5/5] Fix stability in sort/1

The comparisons for descending split were incorrect for
equal elements; should use `>`, not `>=`.  Maybe a copy paste error?
Keysort used the correct comparison and had apparently
gotten more attention.

Using mergel/2 after descending split was not entirely correct.
The correct variant does not exist.  For keysort/1 the possibility
to combine keymergel with argument desc exists, and this is the
correct combination; elements in sorted order in the sublists,
and sublists in reversed order (prefer the tail if equal).
This was solved by reversing the `Rs` list when calling mergel/2.

The optimization for leading equal terms was broken
when transitioning to descending split in the collected
element order had to be reversed, and the run of equal elements
had to be closed to ensure they all end up before the
descending sort elements.
---
 lib/stdlib/src/lists.erl | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl
index 9e35f2f547..97a9514d2f 100644
--- a/lib/stdlib/src/lists.erl
+++ b/lib/stdlib/src/lists.erl
@@ -682,7 +682,7 @@ sort_1(X, [Y | L], R) when X == Y ->
 sort_1(X, [Y | L], R) when X < Y ->
     split_1(X, Y, L, R, []);
 sort_1(X, [Y | L], R) ->
-    split_2(X, Y, L, R, []);
+    split_2(X, Y, L, [], [lists:reverse(R, [])]);
 sort_1(X, [], R) ->
     lists:reverse(R, [X]).
 
@@ -1875,7 +1875,7 @@ split_1_1(X, Y, [Z | L], R, Rs, S) when Z >= Y ->
     split_1_1(Y, Z, L, [X | R], Rs, S);
 split_1_1(X, Y, [Z | L], R, Rs, S) when Z >= X ->
     split_1_1(Z, Y, L, [X | R], Rs, S);
-split_1_1(X, Y, [Z | L], R, Rs, S) when S =< Z ->
+split_1_1(X, Y, [Z | L], R, Rs, S) when Z >= S ->
     split_1(S, Z, L, [], [[Y, X | R] | Rs]);
 split_1_1(X, Y, [Z | L], R, Rs, S) ->
     split_1(Z, S, L, [], [[Y, X | R] | Rs]);
@@ -1883,27 +1883,27 @@ split_1_1(X, Y, [], R, Rs, S) ->
     rmergel([[S], [Y, X | R] | Rs], []).
 
 %% Descending.
-split_2(X, Y, [Z | L], R, Rs) when Z =< Y ->
+split_2(X, Y, [Z | L], R, Rs) when Y > Z ->
     split_2(Y, Z, L, [X | R], Rs);
-split_2(X, Y, [Z | L], R, Rs) when Z =< X ->
+split_2(X, Y, [Z | L], R, Rs) when X > Z ->
     split_2(Z, Y, L, [X | R], Rs);
 split_2(X, Y, [Z | L], [], Rs) ->
     split_2(X, Y, L, [Z], Rs);
 split_2(X, Y, [Z | L], R, Rs) ->
     split_2_1(X, Y, L, R, Rs, Z);
 split_2(X, Y, [], R, Rs) ->
-    mergel([[Y, X | R] | Rs], []).
+    mergel(lists:reverse(Rs, [[Y, X | R]]), []).
 
-split_2_1(X, Y, [Z | L], R, Rs, S) when Z =< Y ->
+split_2_1(X, Y, [Z | L], R, Rs, S) when Y > Z ->
     split_2_1(Y, Z, L, [X | R], Rs, S);
-split_2_1(X, Y, [Z | L], R, Rs, S) when Z =< X ->
+split_2_1(X, Y, [Z | L], R, Rs, S) when X > Z ->
     split_2_1(Z, Y, L, [X | R], Rs, S);
 split_2_1(X, Y, [Z | L], R, Rs, S) when S > Z ->
     split_2(S, Z, L, [], [[Y, X | R] | Rs]);
 split_2_1(X, Y, [Z | L], R, Rs, S) ->
     split_2(Z, S, L, [], [[Y, X | R] | Rs]);
 split_2_1(X, Y, [], R, Rs, S) ->
-    mergel([[S], [Y, X | R] | Rs], []).
+    mergel(lists:reverse(Rs, [[Y, X | R], [S]]), []).
 
 %% merge/1
 
-- 
2.43.0

openSUSE Build Service is sponsored by