HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.
View in English Always switch to English
Der JavaScript-Ausnahmefehler "ungültiges Argument für Array.prototype.sort" tritt auf, wenn das Argument von Array.prototype.sort() (und seinen verwandten Methoden: Array.prototype.toSorted(), TypedArray.prototype.sort(), TypedArray.prototype.toSorted()) weder undefined noch eine Funktion ist, die ihre Operanden vergleicht.
Array.prototype.sort()
Array.prototype.toSorted()
TypedArray.prototype.sort()
TypedArray.prototype.toSorted()
undefined
TypeError: The comparison function must be either a function or undefined (V8-based) TypeError: invalid Array.prototype.sort argument (Firefox) TypeError: non-function passed to Array.prototype.toSorted (Firefox) TypeError: invalid %TypedArray%.prototype.sort argument (Firefox) TypeError: Array.prototype.sort requires the comparator argument to be a function or undefined (Safari) TypeError: Array.prototype.toSorted requires the comparator argument to be a function or undefined (Safari) TypeError: TypedArray.prototype.sort requires the comparator argument to be a function or undefined (Safari) TypeError: TypedArray.prototype.toSorted requires the comparator argument to be a function or undefined (Safari)
TypeError
Das Argument von Array.prototype.sort() (und seinen verwandten Methoden: Array.prototype.toSorted(), TypedArray.prototype.sort(), TypedArray.prototype.toSorted()) sollte entweder undefined oder eine Funktion sein, die ihre Operanden vergleicht.
[1, 3, 2].sort(5); // TypeError students.toSorted("name"); // TypeError
[1, 3, 2].sort(); // [1, 2, 3] [1, 3, 2].sort((a, b) => a - b); // [1, 2, 3] students.toSorted((a, b) => a.name.localeCompare(b.name));