Skip to content

Commit

Permalink
Explicitly pass _comparefn_ to SortCompare
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jan 3, 2022
1 parent a2da384 commit 9c212d8
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -37606,7 +37606,7 @@ <h1>Array.prototype.sort ( _comparefn_ )</h1>
1. Append _kValue_ to _items_.
1. Set _k_ to _k_ + 1.
1. Let _itemCount_ be the number of elements in _items_.
1. [id="step-array-sort"] Sort _items_ using an implementation-defined sequence of calls to SortCompare. If any such call returns an abrupt completion, stop before performing any further calls to SortCompare or steps in this algorithm and return that completion.
1. [id="step-array-sort"] Sort _items_ using an implementation-defined sequence of calls to SortCompare with arguments &laquo; _comparefn_, _a_, _b_ &raquo;, where _a_ and _b_ are the two elements of _items_ to compare. If any such call returns an abrupt completion, stop before performing any further calls to SortCompare or steps in this algorithm and return that completion.
1. Let _j_ be 0.
1. Repeat, while _j_ &lt; _itemCount_,
1. Perform ? Set(_obj_, ! ToString(𝔽(_j_)), _items_[_j_], *true*).
Expand Down Expand Up @@ -37635,7 +37635,7 @@ <h1>Array.prototype.sort ( _comparefn_ )</h1>
There must be some mathematical permutation &pi; of the non-negative integers less than _itemCount_, such that for every non-negative integer _j_ less than _itemCount_, the element <emu-eqn>old[_j_]</emu-eqn> is exactly the same as <emu-eqn>new[&pi;(_j_)]</emu-eqn>.
</li>
<li>
Then for all non-negative integers _j_ and _k_, each less than _itemCount_, if <emu-eqn>SortCompare(old[_j_], old[_k_]) &lt; 0</emu-eqn> (see SortCompare below), then <emu-eqn>&pi;(_j_) &lt; &pi;(_k_)</emu-eqn>.
Then for all non-negative integers _j_ and _k_, each less than _itemCount_, if <emu-eqn>SortCompare(_comparefn_, old[_j_], old[_k_]) &lt; 0</emu-eqn> (see SortCompare below), then <emu-eqn>&pi;(_j_) &lt; &pi;(_k_)</emu-eqn>.
</li>
</ul>
<p>Here the notation <emu-eqn>old[_j_]</emu-eqn> is used to refer to <emu-eqn>_items_[_j_]</emu-eqn> before step <emu-xref href="#step-array-sort"></emu-xref> is executed, and the notation <emu-eqn>new[_j_]</emu-eqn> to refer to <emu-eqn>_items_[_j_]</emu-eqn> after step <emu-xref href="#step-array-sort"></emu-xref> has been executed.</p>
Expand Down Expand Up @@ -37673,14 +37673,12 @@ <h1>Array.prototype.sort ( _comparefn_ )</h1>
<emu-clause id="sec-sortcompare" type="abstract operation">
<h1>
SortCompare (
_comparefn_: *undefined* or a function object,
_x_: unknown,
_y_: unknown,
)
</h1>
<dl class="header">
<dt>description</dt>
<dd>It also has access to the _comparefn_ argument passed to the current invocation of the `sort` method.</dd>
</dl>
<dl class="header"></dl>
<emu-alg>
1. If _x_ and _y_ are both *undefined*, return *+0*<sub>𝔽</sub>.
1. If _x_ is *undefined*, return *1*<sub>𝔽</sub>.
Expand Down Expand Up @@ -39012,27 +39010,41 @@ <h1>%TypedArray%.prototype.sort ( _comparefn_ )</h1>
1. Let _buffer_ be _obj_.[[ViewedArrayBuffer]].
1. Let _len_ be _obj_.[[ArrayLength]].
</emu-alg>
<p>The following version of SortCompare is used by %TypedArray%`.prototype.sort`. It performs a numeric comparison rather than the string comparison used in <emu-xref href="#sec-array.prototype.sort"></emu-xref>.</p>
<p>The abstract operation TypedArraySortCompare takes arguments _x_ and _y_. It also has access to the _comparefn_ and _buffer_ values of the current invocation of the `sort` method. It performs the following steps when called:</p>
<emu-alg>
1. Assert: Both Type(_x_) and Type(_y_) are Number or both are BigInt.
1. If _comparefn_ is not *undefined*, then
1. Let _v_ be ? ToNumber(? Call(_comparefn_, *undefined*, &laquo; _x_, _y_ &raquo;)).
1. If IsDetachedBuffer(_buffer_) is *true*, throw a *TypeError* exception.
1. If _v_ is *NaN*, return *+0*<sub>𝔽</sub>.
1. Return _v_.
1. If _x_ and _y_ are both *NaN*, return *+0*<sub>𝔽</sub>.
1. If _x_ is *NaN*, return *1*<sub>𝔽</sub>.
1. If _y_ is *NaN*, return *-1*<sub>𝔽</sub>.
1. If _x_ &lt; _y_, return *-1*<sub>𝔽</sub>.
1. If _x_ &gt; _y_, return *1*<sub>𝔽</sub>.
1. If _x_ is *-0*<sub>𝔽</sub> and _y_ is *+0*<sub>𝔽</sub>, return *-1*<sub>𝔽</sub>.
1. If _x_ is *+0*<sub>𝔽</sub> and _y_ is *-0*<sub>𝔽</sub>, return *1*<sub>𝔽</sub>.
1. Return *+0*<sub>𝔽</sub>.
</emu-alg>
<emu-note>
<p>Because *NaN* always compares greater than any other value, *NaN* property values always sort to the end of the result when _comparefn_ is not provided.</p>
</emu-note>
<p>Instead of calling SortCompare with arguments &laquo; _comparefn_, _a_, _b_ &raquo;, it calls TypedArraySortCompare with arguments &laquo; _comparefn_, _buffer_, _a_, _b_ &raquo;.</p>

<emu-clause id="sec-typedarraysortcompare" type="abstract operation">
<h1>
TypedArraySortCompare (
_comparefn_: *undefined* or a function object,
_buffer_: an ArrayBuffer or a SharedArrayBuffer,
_x_: a Number or BigInt,
_y_: a Number or BigInt,
)
</h1>
<dl class="header">
<dt>description</dt>
<dd>It performs a numeric comparison rather than the string comparison used in <emu-xref href="#sec-array.prototype.sort"></emu-xref>.</dd>
</dl>
<emu-alg>
1. Assert: Both Type(_x_) and Type(_y_) are Number or both are BigInt.
1. If _comparefn_ is not *undefined*, then
1. Let _v_ be ? ToNumber(? Call(_comparefn_, *undefined*, &laquo; _x_, _y_ &raquo;)).
1. If IsDetachedBuffer(_buffer_) is *true*, throw a *TypeError* exception.
1. If _v_ is *NaN*, return *+0*<sub>𝔽</sub>.
1. Return _v_.
1. If _x_ and _y_ are both *NaN*, return *+0*<sub>𝔽</sub>.
1. If _x_ is *NaN*, return *1*<sub>𝔽</sub>.
1. If _y_ is *NaN*, return *-1*<sub>𝔽</sub>.
1. If _x_ &lt; _y_, return *-1*<sub>𝔽</sub>.
1. If _x_ &gt; _y_, return *1*<sub>𝔽</sub>.
1. If _x_ is *-0*<sub>𝔽</sub> and _y_ is *+0*<sub>𝔽</sub>, return *-1*<sub>𝔽</sub>.
1. If _x_ is *+0*<sub>𝔽</sub> and _y_ is *-0*<sub>𝔽</sub>, return *1*<sub>𝔽</sub>.
1. Return *+0*<sub>𝔽</sub>.
</emu-alg>
<emu-note>
<p>Because *NaN* always compares greater than any other value, *NaN* property values always sort to the end of the result when _comparefn_ is not provided.</p>
</emu-note>
</emu-clause>
</emu-clause>

<emu-clause id="sec-%typedarray%.prototype.subarray">
Expand Down

0 comments on commit 9c212d8

Please sign in to comment.