Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

TypeError: invalid Array.prototype.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

文档标签和贡献者

 此页面的贡献者: fscholz, nbp
 最后编辑者: fscholz,