1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
4 | var aCallable = require('../internals/a-callable');
|
---|
5 | var toObject = require('../internals/to-object');
|
---|
6 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
7 | var deletePropertyOrThrow = require('../internals/delete-property-or-throw');
|
---|
8 | var toString = require('../internals/to-string');
|
---|
9 | var fails = require('../internals/fails');
|
---|
10 | var internalSort = require('../internals/array-sort');
|
---|
11 | var arrayMethodIsStrict = require('../internals/array-method-is-strict');
|
---|
12 | var FF = require('../internals/engine-ff-version');
|
---|
13 | var IE_OR_EDGE = require('../internals/engine-is-ie-or-edge');
|
---|
14 | var V8 = require('../internals/engine-v8-version');
|
---|
15 | var WEBKIT = require('../internals/engine-webkit-version');
|
---|
16 |
|
---|
17 | var test = [];
|
---|
18 | var nativeSort = uncurryThis(test.sort);
|
---|
19 | var push = uncurryThis(test.push);
|
---|
20 |
|
---|
21 | // IE8-
|
---|
22 | var FAILS_ON_UNDEFINED = fails(function () {
|
---|
23 | test.sort(undefined);
|
---|
24 | });
|
---|
25 | // V8 bug
|
---|
26 | var FAILS_ON_NULL = fails(function () {
|
---|
27 | test.sort(null);
|
---|
28 | });
|
---|
29 | // Old WebKit
|
---|
30 | var STRICT_METHOD = arrayMethodIsStrict('sort');
|
---|
31 |
|
---|
32 | var STABLE_SORT = !fails(function () {
|
---|
33 | // feature detection can be too slow, so check engines versions
|
---|
34 | if (V8) return V8 < 70;
|
---|
35 | if (FF && FF > 3) return;
|
---|
36 | if (IE_OR_EDGE) return true;
|
---|
37 | if (WEBKIT) return WEBKIT < 603;
|
---|
38 |
|
---|
39 | var result = '';
|
---|
40 | var code, chr, value, index;
|
---|
41 |
|
---|
42 | // generate an array with more 512 elements (Chakra and old V8 fails only in this case)
|
---|
43 | for (code = 65; code < 76; code++) {
|
---|
44 | chr = String.fromCharCode(code);
|
---|
45 |
|
---|
46 | switch (code) {
|
---|
47 | case 66: case 69: case 70: case 72: value = 3; break;
|
---|
48 | case 68: case 71: value = 4; break;
|
---|
49 | default: value = 2;
|
---|
50 | }
|
---|
51 |
|
---|
52 | for (index = 0; index < 47; index++) {
|
---|
53 | test.push({ k: chr + index, v: value });
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | test.sort(function (a, b) { return b.v - a.v; });
|
---|
58 |
|
---|
59 | for (index = 0; index < test.length; index++) {
|
---|
60 | chr = test[index].k.charAt(0);
|
---|
61 | if (result.charAt(result.length - 1) !== chr) result += chr;
|
---|
62 | }
|
---|
63 |
|
---|
64 | return result !== 'DGBEFHACIJK';
|
---|
65 | });
|
---|
66 |
|
---|
67 | var FORCED = FAILS_ON_UNDEFINED || !FAILS_ON_NULL || !STRICT_METHOD || !STABLE_SORT;
|
---|
68 |
|
---|
69 | var getSortCompare = function (comparefn) {
|
---|
70 | return function (x, y) {
|
---|
71 | if (y === undefined) return -1;
|
---|
72 | if (x === undefined) return 1;
|
---|
73 | if (comparefn !== undefined) return +comparefn(x, y) || 0;
|
---|
74 | return toString(x) > toString(y) ? 1 : -1;
|
---|
75 | };
|
---|
76 | };
|
---|
77 |
|
---|
78 | // `Array.prototype.sort` method
|
---|
79 | // https://tc39.es/ecma262/#sec-array.prototype.sort
|
---|
80 | $({ target: 'Array', proto: true, forced: FORCED }, {
|
---|
81 | sort: function sort(comparefn) {
|
---|
82 | if (comparefn !== undefined) aCallable(comparefn);
|
---|
83 |
|
---|
84 | var array = toObject(this);
|
---|
85 |
|
---|
86 | if (STABLE_SORT) return comparefn === undefined ? nativeSort(array) : nativeSort(array, comparefn);
|
---|
87 |
|
---|
88 | var items = [];
|
---|
89 | var arrayLength = lengthOfArrayLike(array);
|
---|
90 | var itemsLength, index;
|
---|
91 |
|
---|
92 | for (index = 0; index < arrayLength; index++) {
|
---|
93 | if (index in array) push(items, array[index]);
|
---|
94 | }
|
---|
95 |
|
---|
96 | internalSort(items, getSortCompare(comparefn));
|
---|
97 |
|
---|
98 | itemsLength = lengthOfArrayLike(items);
|
---|
99 | index = 0;
|
---|
100 |
|
---|
101 | while (index < itemsLength) array[index] = items[index++];
|
---|
102 | while (index < arrayLength) deletePropertyOrThrow(array, index++);
|
---|
103 |
|
---|
104 | return array;
|
---|
105 | }
|
---|
106 | });
|
---|