W3cubDocs

/JavaScript

Errors: Array sort argument

Message

TypeError: invalid Array.prototype.sort argument (Firefox)

Error type

TypeError

What went wrong?

The argument of Array.prototype.sort() is expected to be either undefined or a function which compares its operands.

Examples

Invalid cases

[1, 3, 2].sort(5);  // TypeError

var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key] || 'asc');  // TypeError

Valid cases

[1, 3, 2].sort();   // [1, 2, 3]


var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key || 'asc']); // [1, 2, 3]

See also

© 2005–2018 Mozilla Developer Network and individual 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