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.

Revision 1036602 of TypeError: invalid Array.prototype.sort argument

  • Revision slug: Web/JavaScript/Reference/Errors/Array_sort_argument
  • Revision title: TypeError: invalid Array.prototype.sort argument
  • Revision id: 1036602
  • Created:
  • Creator: nbp
  • Is current revision? No
  • Comment Document error message "TypeError: invalid Array.prototype.sort argument"

Revision Content

{{jsSidebar("Errors")}}

Message

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

Error type

{{jsxref("TypeError")}}

What went wrong?

The argument of {{jsxref("Array.prototype.sort()")}} is expected to be either undefined or a function which compare 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

  • {{jsxref("Array.prototype.sort()")}}

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
TypeError: invalid Array.prototype.sort argument (Firefox)
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("TypeError")}}</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>The argument of {{jsxref("Array.prototype.sort()")}} is expected to be either undefined or a function which compare its operands.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Invalid_cases">Invalid cases</h3>

<pre class="brush: js example-bad">
[1, 3, 2].sort(5);  // TypeError

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

<h3 id="Valid_cases">Valid cases</h3>

<pre class="brush: js example-good">
[1, 3, 2].sort();   // [1, 2, 3]


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

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Array.prototype.sort()")}}</li>
</ul>
Revert to this revision