[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Errors/Array_sort_argument [Back]  [Original]

TypeError: invalid Array.prototype.sort argument - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

TypeError: invalid Array.prototype.sort argument

JavaScript invalid Array.prototype.sort argument 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

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

Array.prototype.sort() Array.prototype.toSorted()TypedArray.prototype.sort()TypedArray.prototype.toSorted() undefined

js
[1, 3, 2].sort(5); // TypeError
students.toSorted("name"); // TypeError

js
[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));


Web Proxy Viewer  |  New URL  |  Original Page