diff --git a/src/common/algorithm.c b/src/common/algorithm.c index 55a7a927..1d907be3 100644 --- a/src/common/algorithm.c +++ b/src/common/algorithm.c @@ -12,10 +12,10 @@ static bool _stable_sort_merge(char* a, int (*f_lt)(const void* a, const void* b, void* extra), void* extra) { while(a < a_end && b < b_end) { - int res = f_lt(a, b, extra); + int res = f_lt(b, a, extra); // check error if(res == -1) return false; - if(res) { + if(res == 0) { // !(b (a>=b) memcpy(r, a, elem_size); a += elem_size; } else { diff --git a/tests/05_list.py b/tests/05_list.py index 5f6b213c..cae71041 100644 --- a/tests/05_list.py +++ b/tests/05_list.py @@ -88,7 +88,7 @@ assert list(range(5, 1, -2)) == [5, 3] # test sort a = [8, 2, 4, 2, 9] assert a.sort() == None -assert a == [2, 2, 4, 8, 9] +assert (a == [2, 2, 4, 8, 9]), a a = [] assert a.sort() == None diff --git a/tests/95_bugs.py b/tests/95_bugs.py index 63802be0..1916c34d 100644 --- a/tests/95_bugs.py +++ b/tests/95_bugs.py @@ -142,4 +142,10 @@ assert a.__new__ == list.__new__ class A: x: list[int] = [i for i in range(1, 4)] -assert A.x == [1, 2, 3] \ No newline at end of file +assert A.x == [1, 2, 3] + +# stable sort +a = [(0, 1), (1, 1), (1, 2)] +b = sorted(a, key=lambda x: x[0]) +if b != [(0, 1), (1, 1), (1, 2)]: + assert False, b \ No newline at end of file