Did sort() change your original array? Use toSorted instead

sort() sorts the same array in place and returns the same reference, so the original changes. toSorted() returns a new sorted array and leaves the original as it was, and numbers still need a compare function.

Published 3 min read#Web development#Developer tools

You have an array of prices, [40, 1, 5, 200], and you sort it with sort() to show it in a table. It comes out as [1, 200, 40, 5], and when you go back to the original array, it has changed too. Two mistakes in one line. This article explains why that happens, and how toSorted() fixes both.

Why did sort() change the original array?

sort() sorts the elements of the array in place and returns a reference to the same array, now sorted, so no new array is ever made.

That is how MDN's Array.prototype.sort page describes it: the return value is the reference to the original array, now sorted, the array is sorted in place, and no copy is made. So if you write const sorted = prices.sort(), sorted and prices are the same array, and any other code holding prices sees the new order.

Why did 200 come before 40?

sort() without a compare function converts the elements to strings, then compares them character by character in UTF-16 order, so "200" comes before "40" because "2" comes before "4".

The same MDN page gives this example: in a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9". Its code example:

const numberArray = [40, 1, 5, 200];
numberArray.sort(); // [1, 200, 40, 5]
numberArray.sort((a, b) => a - b); // [1, 5, 40, 200]

The compare function (a, b) => a - b returns a negative number when a is smaller, so the order becomes numeric, smallest first.

What is toSorted()?

toSorted() is the copying version of sort(): it returns a new array with the elements sorted in ascending order, and the original array stays as it was.

MDN's toSorted page says it accepts a compare function that determines the order of the elements, points you to the sort() page for the details, and that it has been available across browsers since July 2023. The sort() page itself says: to sort the elements in an array without mutating the original array, use toSorted().

The same page's example:

const values = [1, 10, 21, 2];
const sortedValues = values.toSorted((a, b) => a - b);
console.log(sortedValues); // [1, 2, 10, 21]
console.log(values); // [1, 10, 21, 2]

Watch out: if you do not pass a compare function to toSorted(), the elements are converted to strings and sorted by each character's Unicode code point value, so numbers need the compare function here too.

Why does the difference matter in React?

React asks you to treat arrays in state as read-only and to update them with a new array each time, and sort() breaks that rule because it changes the same array.

React's guide to updating arrays in state says that reverse() and sort() mutate the original array, so you cannot use them directly, and advises you to copy the array first and then make changes to it. toSorted() does the copy and the sort in one step:

const [prices, setPrices] = useState(initialPrices);

function sortByPrice() {
  setPrices(prices.toSorted((a, b) => a - b));
}

This way the old state is never touched, and React receives a new array.

Where do you start now?

Search your project for .sort( and look at what each call works on: if the array comes from state, props, a library or a function parameter, replace it with toSorted(). If the elements are numbers, make sure there is a compare function.

If you sort data so you can group it afterwards, see how to group an array with Object.groupBy, and if you need a full copy of an object with arrays inside it, deep copies with structuredClone. If you are still building your JavaScript foundations, the 2026 developer roadmap lays out what to learn next.

How many .sort( calls in your project run on an array that is not theirs?

FAQ

Does toSorted sort numbers correctly on its own?

No. Without a compare function it converts the elements to strings and sorts them by each character's Unicode code point value. For numbers, pass (a, b) => a - b.

When should I still use plain sort?

When the array is yours alone, nothing else holds a reference to it, and you want to sort it in place without a copy.

Do older browsers support toSorted?

MDN says it has been available across browsers since July 2023. If you must support older browsers, copy the array with slice() or spread, then sort.

Share

More articles