| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julio de 2023.
El mtodo toSorted() devuelve un nuevo array con sus elementos ordenados en orden ascendente sin alterar el array original. Forma parte del objeto Array y es la versin
copying del mtodo sort().
toSorted()
toSorted(compareFn)
compareFnOpcional. Una funcin que determina el orden de los elementos. Si es omitida, los elementos del array son convetidos en string y ordenados por su valor Unicode. Ver sort() para ms informacin.
Un nuevo array con sus elementos ordenados de manera ascendente.
Ver sort() para ms informacin del parametro compareFn.
Cuando se usa en arrays dispersos, el mtodo toSorted() itera las posiciones vacas del array como si tuvieran el valor de undifined.
El mtodo toSorted() es genrico. Solo requiere que el valor de this tenga una propiedad length y propiedades indexadas por enteros.
const months = ["Mar", "Jan", "Feb", "Dec"];
const sortedMonths = months.toSorted();
console.log(sortedMonths); // ['Dec', 'Feb', 'Jan', 'Mar']
console.log(months); // ['Mar', 'Jan', 'Feb', 'Dec']
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]
Para ms ejemplos de uso, ver sort().
Las posiciones vacas son ordenadas como si tuvieran el valor undefined. Siempre son ubicadas al final del array y no se invoca compareFn para ellas.
console.log(["a", "c", , "b"].toSorted()); // ['a', 'b', 'c', undefined]
console.log([, undefined, "a", "b"].toSorted()); // ["a", "b", undefined, undefined]
El mtodo toSorted() lee la propiedad length de this. Luego recopila todas las propiedades existentes indexadas por enteros en el rango de 0 a length - 1, las ordena y las escribe en un nuevo array.
const arrayLike = {
length: 3,
unrelated: "foo",
0: 5,
2: 4,
3: 3, // ignorado por toSorted() dado que length es 3
};
console.log(Array.prototype.toSorted.call(arrayLike));
// [4, 5, undefined]
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.tosorted |
Array.prototype.toSorted in core-jsArray.prototype.sort()Array.prototype.toReversed()Array.prototype.toSpliced()Array.prototype.with()TypedArray.prototype.toSorted()This page was last modified on 29 jun 2025 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()Array.prototype.toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |