Loading...
const byPrice = products
.sort((a, b) => a.price - b.price);const byPrice = products
.toSorted((a, b) => a.price - b.price);sort() reorders products itself; toSorted() returns a sorted copy and leaves the source alone.
toSorted() on numbers with no comparator and getting string order.toSorted((a, b) => a - b).toSorted() instead of sort() on state.[...arr].sort().sort() and toSorted()?sort() reorders the array in place. toSorted() returns a new sorted array and leaves the original untouched.
toSorted() stable?Yes. Like sort(), it keeps equal elements in their original order.