From 87cb50bbce7ae4b8c8b62afea4160f226d7f4b4e Mon Sep 17 00:00:00 2001 From: Michael Dyck Date: Thu, 11 Feb 2021 10:43:36 -0500 Subject: [PATCH] Editorial: Factor out SortIndexedProperties --- spec.html | 144 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 70 deletions(-) diff --git a/spec.html b/spec.html index 069bd25bbb0..5ff1b32c8b3 100644 --- a/spec.html +++ b/spec.html @@ -37635,76 +37635,8 @@

Array.prototype.sort ( _comparefn_ )

1. Let _ySmaller_ be ! IsLessThan(_yString_, _xString_, *true*). 1. If _ySmaller_ is *true*, return *1*𝔽. 1. Return *+0*𝔽. - 1. Let _items_ be a new empty List. - 1. Let _k_ be 0. - 1. Repeat, while _k_ < _len_, - 1. Let _Pk_ be ! ToString(𝔽(_k_)). - 1. Let _kPresent_ be ? HasProperty(_obj_, _Pk_). - 1. If _kPresent_ is *true*, then - 1. Let _kValue_ be ? Get(_obj_, _Pk_). - 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 Record. - 1. Let _j_ be 0. - 1. Repeat, while _j_ < _itemCount_, - 1. Perform ? Set(_obj_, ! ToString(𝔽(_j_)), _items_[_j_], *true*). - 1. Set _j_ to _j_ + 1. - 1. Repeat, while _j_ < _len_, - 1. Perform ? DeletePropertyOrThrow(_obj_, ! ToString(𝔽(_j_))). - 1. Set _j_ to _j_ + 1. - 1. Return _obj_. + 1. Return ? SortIndexedProperties(_obj_, _len_, _SortCompare_). -

The sort order is the ordering of _items_ after completion of step of the algorithm above. The result of the `sort` function is then determined as follows:

-

The sort order is implementation-defined if any of the following conditions is true:

- -

Unless the sort order is specified above to be implementation-defined, _items_ must satisfy all of the following conditions after executing step of the algorithm above:

- -

Here the notation old[_j_] is used to refer to _items_[_j_] before step is executed, and the notation new[_j_] to refer to _items_[_j_] after step has been executed.

-

An abstract closure or function _comparator_ is a consistent comparator for a set of values _S_ if all of the requirements below are met for all values _a_, _b_, and _c_ (possibly the same value) in the set _S_: The notation _a_ <C _b_ means _comparator_(_a_, _b_) < 0; _a_ =C _b_ means _comparator_(_a_, _b_) = 0 (of either sign); and _a_ >C _b_ means _comparator_(_a_, _b_) > 0.

- - -

The above conditions are necessary and sufficient to ensure that _comparator_ divides the set _S_ into equivalence classes and that these equivalence classes are totally ordered.

-

Because non-existent property values always compare greater than *undefined* property values, and *undefined* always compares greater than any other value, *undefined* property values always sort to the end of the result, followed by non-existent property values.

@@ -37714,6 +37646,77 @@

Array.prototype.sort ( _comparefn_ )

The `sort` function is intentionally generic; it does not require that its *this* value be an Array. Therefore, it can be transferred to other kinds of objects for use as a method.

+ + +

+ SortIndexedProperties ( + _obj_: an Object, + _len_: a non-negative integer, + _SortCompare_: an Abstract Closure with two parameters, + ): either a normal completion containing an Object or an abrupt completion +

+
+
+ + 1. Let _items_ be a new empty List. + 1. Let _k_ be 0. + 1. Repeat, while _k_ < _len_, + 1. Let _Pk_ be ! ToString(𝔽(_k_)). + 1. Let _kPresent_ be ? HasProperty(_obj_, _Pk_). + 1. If _kPresent_ is *true*, then + 1. Let _kValue_ be ? Get(_obj_, _Pk_). + 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 Record. + 1. Let _j_ be 0. + 1. Repeat, while _j_ < _itemCount_, + 1. Perform ? Set(_obj_, ! ToString(𝔽(_j_)), _items_[_j_], *true*). + 1. Set _j_ to _j_ + 1. + 1. Repeat, while _j_ < _len_, + 1. Perform ? DeletePropertyOrThrow(_obj_, ! ToString(𝔽(_j_))). + 1. Set _j_ to _j_ + 1. + 1. Return _obj_. + +

The sort order is the ordering of _items_ after completion of step of the algorithm above. The sort order is implementation-defined if _SortCompare_ is not a consistent comparator for the elements of _items_. When SortIndexedProperties is invoked by Array.prototype.sort, the sort order is also implementation-defined if _comparefn_ is *undefined*, and all applications of ToString, to any specific value passed as an argument to _SortCompare_, do not produce the same result.

+

Unless the sort order is specified to be implementation-defined, it must satisfy all of the following conditions:

+ +

Here the notation old[_j_] is used to refer to _items_[_j_] before step is executed, and the notation new[_j_] to refer to _items_[_j_] after step has been executed.

+

An abstract closure or function _comparator_ is a consistent comparator for a set of values _S_ if all of the requirements below are met for all values _a_, _b_, and _c_ (possibly the same value) in the set _S_: The notation _a_ <C _b_ means _comparator_(_a_, _b_) < 0; _a_ =C _b_ means _comparator_(_a_, _b_) = 0 (of either sign); and _a_ >C _b_ means _comparator_(_a_, _b_) > 0.

+ + +

The above conditions are necessary and sufficient to ensure that _comparator_ divides the set _S_ into equivalence classes and that these equivalence classes are totally ordered.

+
+
@@ -39011,7 +39014,7 @@

%TypedArray%.prototype.some ( _callbackfn_ [ , _thisArg_ ] )

%TypedArray%.prototype.sort ( _comparefn_ )

%TypedArray%`.prototype.sort` is a distinct function that, except as described below, implements the same requirements as those of `Array.prototype.sort` as defined in . The implementation of the %TypedArray%`.prototype.sort` specification may be optimized with the knowledge that the *this* value is an object that has a fixed length and whose integer-indexed properties are not sparse.

This function is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.

-

Upon entry, the following steps are performed to initialize evaluation of the `sort` function. These steps are used instead of steps in :

+

The following steps are performed:

1. If _comparefn_ is not *undefined* and IsCallable(_comparefn_) is *false*, throw a *TypeError* exception. 1. Let _obj_ be the *this* value. @@ -39034,6 +39037,7 @@

%TypedArray%.prototype.sort ( _comparefn_ )

1. If _x_ is *-0*𝔽 and _y_ is *+0*𝔽, return *-1*𝔽. 1. If _x_ is *+0*𝔽 and _y_ is *-0*𝔽, return *1*𝔽. 1. Return *+0*𝔽. + 1. Return ? SortIndexedProperties(_obj_, _len_, _SortCompare_).

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.