The JavaScript exception "invalid Array.prototype.sort argument" occurs when the argument of Array.prototype.sort()
isn't either undefined
or a function which compares its operands.
The JavaScript exception "invalid Array.prototype.sort argument" occurs when the argument of Array.prototype.sort()
isn't either undefined
or a function which compares its operands.
TypeError: The comparison function must be either a function or undefined (V8-based) TypeError: invalid Array.prototype.sort argument (Firefox) TypeError: Array.prototype.sort requires the comparator argument to be a function or undefined (Safari)
The argument of Array.prototype.sort()
is expected to be either undefined
or a function which compares its operands.
[1, 3, 2].sort(5); // TypeError
[1, 3, 2].sort(); // [1, 2, 3] [1, 3, 2].sort((a, b) => a - b); // [1, 2, 3]
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Array_sort_argument