AI basics & verificationPractical AI workflows

Why AI-written number sorting puts 11 before 2: comparator, input contract and mutation

A proposed sort() call preserves three numbers but arranges them 11,2,3. No exception and an unchanged element count do not establish correctness. Define whether the objects are numeric values, identifiers or display strings before inspecting order and any silent input conversion.

Archival syntax-highlighted JavaScript on a screen, not a screenshot of this sorting function running
Illustrative photo: Gabriel Heinzer · Unsplash License

The workflow at a glance

JavaScript sort() defaults to string-based UTF-16 ordering and mutates its array. For this exercise’s finite-Number contract, validate the input, copy it and use(a,b)=>a-b. Reject strings and non-finite values. Identifiers are not automatically numeric data, and sorting must not quietly deduplicate.

Make ascending order a checkable contract

MDN’s Array.sort documentation distinguishes default string comparison and in-place mutation. The comparator can produce the wrong order; mutation can independently break a later step that still needs the input order.

This is an editorial JavaScript review exercise, not a model request. Accept only an array of finite Number values, return numeric ascending order in a new array, preserve duplicates and leave the input unchanged. Strings, NaN and Infinity are rejected. This is the specimen’s contract rather than a universal project data policy.

Separate three data meanings

Numeric values

Compare magnitude after defining type and valid range.

Identifiers

02 and 2 may identify different objects; do not collapse them numerically.

Display text

Language or display rules may apply instead of numeric magnitude.

Include both a failure and an accidental pass

A is[2,11,3]. Default sorting produces[11,2,3], while the numeric reference is[2,3,11]. The leading character 1 sorts before 2, explaining why a plausible ascending-looking result is not numeric ascending order.

B is[-2,-11,0,5]. Default sorting happens to produce the same[-11,-2,0,5] as the numeric reference. Testing B alone therefore misses A’s failure. Empty arrays, duplicates and mixed types test other parts of the contract.

Original inputs and human references
InputRequired resultWhat it checks
[2,11,3][2,3,11]Default string ordering fails
[-2,-11,0,5][-11,-2,0,5]An accidental pass does not prove a general rule
[3,3,1][1,3,3]Preserve duplicates
[][] in a new arrayEmpty input and identity
[2,'11',3]RejectNo silent mixed-type conversion
[2,NaN,3]RejectNo non-finite comparison values
Array(2)RejectEmpty slots are not finite Number values

The reference fixes order, input and copying together

The following is a human reference, not a repair generated by a model. Validate the array, then inspect a copy before sorting. Copying exposes empty slots as undefined, so sparse input is rejected too. Adding a subtraction comparator while admitting strings, missing values or infinities still fails this input contract.

The copy is appropriate for this Number-array task. With an object array, a shallow copy would not make its nested objects independent. Stable keys, missing fields and equal-value rules would need their own definition. This snippet is not a general data-cleaning library.

Reference implementation for this contract

function numericAscending(values) {
  if (!Array.isArray(values) || ![...values].every(
    value => typeof value === 'number' && Number.isFinite(value)
  )) {
    throw new TypeError('Expected finite Number values');
  }
  return [...values].sort((a, b) => a - b);
}

const input = [2, 11, 3];
const output = numericAscending(input);
// output: [2, 3, 11]
// input remains: [2, 11, 3]
Editorial reference implementation; inputs, outputs and rejection behavior still require execution.

Only the comparator was repaired

Editorial failure specimen

Convert every input with Number before subtraction sorting, turning an empty string into 0 and the identifier 02 into 2.

Reference correction

Sort only the finite Number input this contract permits; reject other types or obtain an explicit conversion rule.

Fixing order does not authorize silently changing type or identity.

Give the review counterexamples, mutation and rejection checks

Instead of requesting an ascending function alone, supply all seven specimens and require expected outputs, mutation behavior and rejected inputs separately. Execute them after code review, recording failed, unrun and passing items distinctly.

Check identity as well as contents: the original values must remain unchanged and the result must be a different array. Duplicates retain their multiplicity. An empty input still requires a new empty output, despite having no visible values to compare.

A review task with acceptance conditions

Review numericAscending for this contract: an array of finite JavaScript Number values; numeric ascending output in a new array; preserve duplicates; leave input unchanged. Use all seven cases above. Reject mixed strings and non-finite values rather than silently converting them. Explain why the default sort fails A but happens to pass B. Report unexecuted proposals separately from observed checks.
Original review material, not an actual model call.

Checks to execute

  • Valid outputs match the human reference order.
  • Input contents remain unchanged after the call.
  • Output is not the input array itself.
  • Duplicate multiplicity and element count survive.
  • Mixed types and NaN are rejected under the contract.

Correct sorting does not prove original numeric precision

Finite Number values do not guarantee exact representation of every large integer. Read the floating-point and tolerance audit for precision boundaries, and JSON-to-Excel numeric spelling checks for file handoff.

If the data are identifiers 02,2,11, define identity and display order before sorting. Numeric sorting is not identifier normalization or deduplication. This result covers the stated contract, rather than all locale collation, object ordering or file-import behavior.

Common questions

Why did the original array change when sort() was assigned to a new variable?

A variable name is not a new array. sort() returns the same reference that it mutates. This reference copies the input before sorting.

Can a comparator return true or false?

That is not a general numeric ascending comparator. The comparison must express less than, equal and greater than; a Boolean alone cannot represent all three relations.

Should sorting remove repeated values?

Not in this contract, which preserves them. Deduplication needs a separate identity rule and must not be silently added.

Further reading & sources

Related articles