Truly independent web browser
LibJS: Pass the later element first when sorting compares two elements
Andreas Klingc5c3412ffac8
diff --git a/Libraries/LibJS/Rust/src/runtime/array_prototype.rs b/Libraries/LibJS/Rust/src/runtime/array_prototype.rsindex cd75b2acd1a..e7a9627a5c2 100644--- a/Libraries/LibJS/Rust/src/runtime/array_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/array_prototype.rs@@ -2034,14 +2034,17 @@ pub fn array_merge_sort(let x = left.get(left_index).expect("the index is below the size");let y = right.get(right_index).expect("the index is below the size");- let comparison_result = compare_func(x, y)?;+ // INTEROP: The later element is passed as the first argument, and the earlier one stays first unless it compares+ // below it. Most other engines do the same, and server rendered pages depend on it when they sort with+ // an inconsistent comparator such as `() => 1`, which then has to leave the order unchanged.+ let comparison_result = compare_func(y, x)?;- if comparison_result <= 0.0 {- append(x);- left_index += 1;- } else {+ if comparison_result < 0.0 {append(y);right_index += 1;+ } else {+ append(x);+ left_index += 1;}}diff --git a/Tests/LibJS/Runtime/builtins/Array/Array.prototype.sort.js b/Tests/LibJS/Runtime/builtins/Array/Array.prototype.sort.jsindex db2f72a5f70..5b74c7ba0ae 100644--- a/Tests/LibJS/Runtime/builtins/Array/Array.prototype.sort.js+++ b/Tests/LibJS/Runtime/builtins/Array/Array.prototype.sort.js@@ -172,4 +172,24 @@ describe("Array.prototype.sort", () => {);Array.prototype.sort.call(obj);});++ test("inconsistent comparators sort the way websites expect", () => {+ const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];+ const reversed = numbers.toReversed();++ expect([1, 2].sort(() => 1)).toEqual([1, 2]);+ expect([1, 2].sort(() => -1)).toEqual([2, 1]);+ expect(numbers.slice().sort(() => 1)).toEqual(numbers);+ expect(numbers.slice().sort(() => -1)).toEqual(reversed);+ expect(numbers.toSorted(() => 1)).toEqual(numbers);+ expect(numbers.toSorted(() => -1)).toEqual(reversed);++ // A comparator that returns a boolean never reports "less than".+ expect(reversed.slice().sort((a, b) => a > b)).toEqual(reversed);+ expect(numbers.slice().sort((a, b) => a < b)).toEqual(numbers);++ // A comparator that only looks at its first argument.+ expect([1, 2, 3, 4, 5].sort(a => (a === 3 ? -1 : 1))).toEqual([3, 1, 2, 4, 5]);+ expect([1, 2, 3, 4, 5].sort(a => (a === 3 ? 1 : -1))).toEqual([5, 4, 2, 1, 3]);+ });});diff --git a/Tests/LibJS/Runtime/builtins/TypedArray/TypedArray.prototype.sort.js b/Tests/LibJS/Runtime/builtins/TypedArray/TypedArray.prototype.sort.jsindex 3c44ca37973..f684b1d4a0b 100644--- a/Tests/LibJS/Runtime/builtins/TypedArray/TypedArray.prototype.sort.js+++ b/Tests/LibJS/Runtime/builtins/TypedArray/TypedArray.prototype.sort.js@@ -47,7 +47,7 @@ test("basic functionality", () => {expect(typedArray[1]).toBe(2);expect(typedArray[2]).toBe(3);- expect(typedArray.sort(() => 1)).toBe(typedArray);+ expect(typedArray.sort(() => -1)).toBe(typedArray);expect(typedArray[0]).toBe(3);expect(typedArray[1]).toBe(2);expect(typedArray[2]).toBe(1);@@ -66,7 +66,7 @@ test("basic functionality", () => {expect(typedArray[1]).toBe(2n);expect(typedArray[2]).toBe(3n);- expect(typedArray.sort(() => 1)).toBe(typedArray);+ expect(typedArray.sort(() => -1)).toBe(typedArray);expect(typedArray[0]).toBe(3n);expect(typedArray[1]).toBe(2n);expect(typedArray[2]).toBe(1n);