Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2023.
The toSorted() method of Array instances is the copying version of the sort() method. It returns a new array with the elements sorted in ascending order.
A function that determines the order of the elements. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value. See sort() for more information.
A new array with the elements sorted in ascending order.
See sort() for more information on the compareFn parameter.
When used on sparse arrays, the toSorted() method iterates empty slots as if they have the value undefined.
The toSorted() method is generic. It only expects the this value to have a length property and integer-keyed properties.
For more usage examples, see sort().
Empty slots are sorted as if they have the value undefined. They are always sorted to the end of the array and compareFn is not called for them.
The toSorted() method reads the length property of this. It then collects all existing integer-keyed properties in the range of 0 to length - 1, sorts them, and writes them into a new array.
| ECMAScript® 2027 Language Specification # sec-array.prototype.tosorted |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.