JS Built-ins
← Array193 / 226
ArrayArray.prototype.toSortedintermediate

toSorted Does Not Mutate

πŸ“– Usage Example
const original = ['c', 'a', 'b']
const sorted = original.toSorted()
original  // β†’ ['c', 'a', 'b']  (unchanged)
sorted    // β†’ ['a', 'b', 'c']

toSorted() is the immutable counterpart of sort() β€” the original array is never changed.

Array.prototype.toSorted β€” Immutability

Unlike sort, toSorted never mutates the original array. The original remains unchanged after the call.

Implement checkSortedImmutable which calls toSorted() but returns the original array, proving it was not changed.

πŸ’‘ Hints (2)
  • β€’ toSorted always returns a brand-new array β€” the original is safe to return as-is
  • β€’ You can ignore the return value of toSorted here