Files
scriptc/tests/corpus/2701-array-sort-snapshot.ts
T
Chris Tate 4b241f77d7 Replace quadratic array sorting with stable merge sort (#296)
* Fix Sandbox command transport and pinned native setup

Build the Linux LLVM helper and runtime pack before running both test lanes, and align native cache contracts with the current target policy. Full managed Sandbox gate passed.

* Replace quadratic array sorting with stable merge sort

Cover sort snapshots, consistent-comparator results, stable ties, and comparator complexity with Node differential fixtures. Document differences from V8 TimSort.
2026-09-12 16:05:16 -05:00

47 lines
1.1 KiB
TypeScript

const receiver = [3, 1, 2];
const returned = receiver.sort((a, b) => a - b);
console.log(returned === receiver, receiver.join(","), returned.join(","));
const mutatedReceiver = [3, 1, 2];
let changedReceiver = false;
mutatedReceiver.sort((a, b) => {
if (!changedReceiver) {
changedReceiver = true;
mutatedReceiver[0] = 99;
}
return a - b;
});
console.log(mutatedReceiver.join(","));
const source = [3, 1, 2];
let changed = false;
const sorted = source.toSorted((a, b) => {
if (!changed) {
changed = true;
source[0] = 99;
source.push(4);
}
return a - b;
});
console.log(sorted.join(","));
console.log(source.join(","));
const stable = [
{ key: "b", order: 1 },
{ key: "a", order: 2 },
{ key: "b", order: 3 },
{ key: "a", order: 4 },
];
const byKey = stable.toSorted((a, b) => a.key.localeCompare(b.key));
console.log(byKey.map((entry) => entry.order).join(","));
console.log(stable.map((entry) => entry.order).join(","));
const throwing = [3, 1, 2];
try {
throwing.sort((a, b): number => {
throw new Error("stop");
});
} catch (error) {
console.log(error instanceof Error, throwing.join(","));
}