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