A separate result array can still leave the input changed

A new array can still hide changed input. Check the returned array and the caller's unchanged input separately before accepting a sorting change. Save the slide (PNG)
For example, a caller’s array contains A with score 10, then B with score 20, in the order needed for a separate display. Sorting that array before copying it returns a separate, correctly ranked result but changes the caller’s display order.
Both implementations below produce [B, A]:
function rankInPlace(records) {
return records.sort((a, b) => b.score - a.score);
}
function rankThenCopy(records) {
records.sort((a, b) => b.score - a.score);
return [...records];
}
JavaScript’s sort() rearranges the original array. The first function returns that array; the second returns a new one after the input has changed. Any other reference to the input now sees [B, A] too.
A separate result array does not establish input preservation.
Test result identity and input preservation separately. If callers need their input preserved, make that explicit before accepting the change. If mutation is intentionally part of the API, specify that instead.
Write the requirement before choosing the implementation
“Return records sorted by score” leaves several decisions open. It says nothing about ownership of the input, whether the result is a separate array, or whether records themselves can change.
For this change, use a bounded requirement:
Given an array of records with distinct numeric scores, return records with the same values in descending score order in a separate array. Leave the caller’s input order and record values unchanged. The result may contain the original record objects or equal-value copies.
That final sentence makes object identity explicit: preserving the original record identities is not required. The assertions below permit cloned records, but do not demand them. A new array can contain references to the existing records without changing them.
Array ownership and record ownership are separate decisions.
Use a four-check acceptance contract
This checklist connects each part of the requirement to an observable check. The executable test combines the last two checks in its final assertion.
| Check | What to assert | What it catches |
|---|---|---|
| Ranking | The result contains B:20, then A:10 |
Wrong order, missing records, or changed output values for these records |
| Array identity | The result is not the input array | Returning the caller’s array instead of a separate result |
| Caller-visible order | A retained input alias still shows A, then B |
In-place reordering |
| Caller-visible values | Records visible through that alias still have their original values | Persistent changes to those input records |
The identity and preservation checks do different work. rankThenCopy passes the first two checks while violating the third.
Check the caller’s retained view
The following JavaScript example uses Node’s built-in assertion module. The records are flat objects, so copying their fields captures their values. For nested records, extend the snapshot strategy if nested-data preservation is required.
import assert from 'node:assert/strict';
function checkRankingContract(rank) {
const input = [
{ id: 'A', score: 10 },
{ id: 'B', score: 20 },
];
// Another part of the caller still holds this array.
const retainedAlias = input;
// Capture values, not another reference to the same records.
const before = input.map(record => ({ ...record }));
const result = rank(input);
assert.deepStrictEqual(
result,
[
{ id: 'B', score: 20 },
{ id: 'A', score: 10 },
],
'Return every record in descending score order',
);
assert.notStrictEqual(
result,
input,
'Return a separate array',
);
assert.deepStrictEqual(
retainedAlias,
before,
'Preserve caller-visible input order and record values',
);
}
// One implementation consistent with this requirement.
function rankWithoutReorderingInput(records) {
return [...records].sort((a, b) => b.score - a.score);
}
// Deliberately violates input preservation despite returning a new array.
function rankThenCopy(records) {
records.sort((a, b) => b.score - a.score);
return [...records];
}
checkRankingContract(rankWithoutReorderingInput);
Here, retainedAlias === input. The alias illustrates the caller’s perspective; it detects nothing beyond comparing input with the same independent snapshot.
The snapshot does the detection work. Writing const before = input would merely create another alias, allowing both references to reflect the same mutation.
Shared records remain shared after return
The passing implementation copies the array, not its records. A caller that later edits result[0].score can also change the corresponding input record. Preventing that requires an additional isolation requirement and additional tests; it should not be silently bundled into “return a separate array.”
I would accept shared, unchanged records for the requirement above. I would require stronger isolation only when the caller needs to edit returned records independently.
This test leaves tied scores, empty arrays, missing scores, and invalid numeric values untested. Define tie ordering separately if callers need it. Because the test observes input after the call, it also misses mutations reversed before the assertions run and changes to external state.
Try the counterexample before accepting the change
- Replace the final call with
checkRankingContract(rankInPlace). Ranking should pass; the separate-array assertion should fail. - Replace it with
checkRankingContract(rankThenCopy). Ranking and array identity should pass; the combined input-order-and-values assertion should fail.
Save the field guide

A field guide distilled from this article. The text version follows below.
Text version
- Check the ranking: The result should contain B with score 20, then A with score 10. Compare the full result with the expected records to catch wrong order, missing records, or changed output values.
- Check array identity: A correctly sorted result may still be the caller's original array. Assert that the result is not the input array to verify that the returned array is separate.
- Check caller-visible order: Even a separate result can leave the caller's input reordered. Compare the retained input with an independent pre-call snapshot to detect in-place reordering.
- Check caller-visible values: The input records may retain changed values after the call. Compare their values with the pre-call snapshot, extending the snapshot for nested data when preservation requires it.
Illustrative counterexample: sort, then copy: Starting with A:10, B:20, rankThenCopy returns a separate array ordered B:20, A:10. Ranking and array identity pass, but the caller's input order changes too.
Copying the array does not isolate its records from later edits. These checks also miss mutations reversed before assertions run and changes to external state.