11'use strict' ;
2- var aFunction = require ( '../internals/a-function' ) ;
3- var toObject = require ( '../internals/to-object' ) ;
4- var toLength = require ( '../internals/to-length' ) ;
5- var fails = require ( '../internals/fails' ) ;
6- var arrayMethodIsStrict = require ( '../internals/array-method-is-strict' ) ;
7- var FF = require ( '../internals/engine-ff-version' ) ;
8- var IE_OR_EDGE = require ( '../internals/engine-is-ie-or-edge' ) ;
9- var V8 = require ( '../internals/engine-v8-version' ) ;
10- var WEBKIT = require ( '../internals/engine-webkit-version' ) ;
11-
12- var test = [ ] ;
13- var nativeSort = test . sort ;
142var floor = Math . floor ;
153
16- // IE8-
17- var FAILS_ON_UNDEFINED = fails ( function ( ) {
18- test . sort ( undefined ) ;
19- } ) ;
20- // V8 bug
21- var FAILS_ON_NULL = fails ( function ( ) {
22- test . sort ( null ) ;
23- } ) ;
24- // Old WebKit
25- var STRICT_METHOD = arrayMethodIsStrict ( 'sort' ) ;
26-
27- var STABLE_SORT = ! fails ( function ( ) {
28- // feature detection can be too slow, so check engines versions
29- if ( V8 ) return V8 < 70 ;
30- if ( FF && FF > 3 ) return ;
31- if ( IE_OR_EDGE ) return true ;
32- if ( WEBKIT ) return WEBKIT < 603 ;
33-
34- var result = '' ;
35- var code , chr , value , index ;
36-
37- // generate an array with more 512 elements (Chakra and old V8 fails only in this case)
38- for ( code = 65 ; code < 76 ; code ++ ) {
39- chr = String . fromCharCode ( code ) ;
40- switch ( code ) {
41- case 66 : case 69 : case 70 : case 72 : value = 3 ; break ;
42- case 68 : case 71 : value = 4 ; break ;
43- default : value = 2 ;
44- }
45-
46- for ( index = 0 ; index < 47 ; index ++ ) {
47- test . push ( { k : chr + index , v : value } ) ;
48- }
49- }
50-
51- test . sort ( function ( a , b ) { return b . v - a . v ; } ) ;
52-
53- for ( index = 0 ; index < test . length ; index ++ ) {
54- chr = test [ index ] . k . charAt ( 0 ) ;
55- if ( result . charAt ( result . length - 1 ) !== chr ) result += chr ;
56- }
57-
58- return result !== 'DGBEFHACIJK' ;
59- } ) ;
60-
61- var FORCED = FAILS_ON_UNDEFINED || ! FAILS_ON_NULL || ! STRICT_METHOD || ! STABLE_SORT ;
62-
634var mergeSort = function ( array , comparefn ) {
645 var length = array . length ;
656 var middle = floor ( length / 2 ) ;
@@ -78,7 +19,7 @@ var insertionSort = function (array, comparefn) {
7819 while ( i < length ) {
7920 j = i ;
8021 element = array [ i ] ;
81- while ( j && sortCompare ( array [ j - 1 ] , element , comparefn ) > 0 ) {
22+ while ( j && comparefn ( array [ j - 1 ] , element ) > 0 ) {
8223 array [ j ] = array [ -- j ] ;
8324 }
8425 if ( j !== i ++ ) array [ j ] = element ;
@@ -94,45 +35,11 @@ var merge = function (left, right, comparefn) {
9435
9536 while ( lindex < llength || rindex < rlength ) {
9637 if ( lindex < llength && rindex < rlength ) {
97- result . push ( sortCompare ( left [ lindex ] , right [ rindex ] , comparefn ) <= 0 ? left [ lindex ++ ] : right [ rindex ++ ] ) ;
38+ result . push ( comparefn ( left [ lindex ] , right [ rindex ] ) <= 0 ? left [ lindex ++ ] : right [ rindex ++ ] ) ;
9839 } else {
9940 result . push ( lindex < llength ? left [ lindex ++ ] : right [ rindex ++ ] ) ;
10041 }
10142 } return result ;
10243} ;
10344
104- var sortCompare = function ( x , y , comparefn ) {
105- if ( y === undefined ) return - 1 ;
106- if ( x === undefined ) return 1 ;
107- if ( comparefn !== undefined ) {
108- return + comparefn ( x , y ) || 0 ;
109- } return String ( x ) > String ( y ) ? 1 : - 1 ;
110- } ;
111-
112- // `Array.prototype.sort` method
113- // https://tc39.es/ecma262/#sec-array.prototype.sort
114- module . exports = FORCED ? function sort ( comparefn ) {
115- if ( comparefn !== undefined ) aFunction ( comparefn ) ;
116-
117- var array = toObject ( this ) ;
118-
119- if ( STABLE_SORT ) return comparefn === undefined ? nativeSort . call ( array ) : nativeSort . call ( array , comparefn ) ;
120-
121- var items = [ ] ;
122- var arrayLength = toLength ( array . length ) ;
123- var itemsLength , index ;
124-
125- for ( index = 0 ; index < arrayLength ; index ++ ) {
126- if ( index in array ) items . push ( array [ index ] ) ;
127- }
128-
129- // TODO: use something more complex like timsort?
130- items = mergeSort ( items , comparefn ) ;
131- itemsLength = items . length ;
132- index = 0 ;
133-
134- while ( index < itemsLength ) array [ index ] = items [ index ++ ] ;
135- while ( index < arrayLength ) delete array [ index ++ ] ;
136-
137- return array ;
138- } : nativeSort ;
45+ module . exports = mergeSort ;
0 commit comments