Skip to content

Commit 57ce9d2

Browse files
refactor: update sort logic and insertion place
1 parent 7ede885 commit 57ce9d2

File tree

1 file changed

+47
-54
lines changed
  • lib/node_modules/@stdlib/array/fixed-endian-factory/lib

1 file changed

+47
-54
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -581,60 +581,6 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
581581
}
582582
return true;
583583
});
584-
/**
585-
* Sorts an array in-place.
586-
*
587-
* @private
588-
* @name sort
589-
* @memberof TypedArray.prototype
590-
* @type {Function}
591-
* @param {Function} compareFcn - comparison function
592-
* @throws {TypeError} `this` must be a typed array
593-
* @throws {TypeError} first argument must be a function
594-
* @returns {TypedArray} sorted array
595-
*/
596-
setReadOnly( TypedArray.prototype, 'sort', function sort( compareFcn ) {
597-
var minIdx;
598-
var minVal;
599-
var val;
600-
var buf;
601-
var i;
602-
var j;
603-
604-
if ( !isTypedArray( this ) ) {
605-
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
606-
}
607-
if ( arguments.length === 0 ) {
608-
compareFcn = function defaultCompareFcn( a, b ) {
609-
if ( a > b ) {
610-
return 1;
611-
}
612-
if ( a < b ) {
613-
return -1;
614-
}
615-
return 0;
616-
};
617-
} else if ( !isFunction( compareFcn ) ) {
618-
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
619-
}
620-
buf = this._buffer;
621-
for ( i = 0; i < this._length-1; i++ ) {
622-
minIdx = i;
623-
minVal = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
624-
for ( j = i+1; j < this._length; j++ ) {
625-
val = buf[ GETTER ]( j * BYTES_PER_ELEMENT, this._isLE );
626-
if ( compareFcn( minVal, val ) > 0 ) {
627-
minIdx = j;
628-
minVal = val;
629-
}
630-
}
631-
if ( i !== minIdx ) {
632-
buf[ SETTER ]( minIdx * BYTES_PER_ELEMENT, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE );
633-
buf[ SETTER ]( i * BYTES_PER_ELEMENT, minVal, this._isLE );
634-
}
635-
}
636-
return this;
637-
});
638584

639585
/**
640586
* Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
@@ -1101,6 +1047,53 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
11011047
return false;
11021048
});
11031049

1050+
/**
1051+
* Sorts an array in-place.
1052+
*
1053+
* @private
1054+
* @name sort
1055+
* @memberof TypedArray.prototype
1056+
* @type {Function}
1057+
* @param {Function} [compareFcn] - comparison function
1058+
* @throws {TypeError} `this` must be a typed array
1059+
* @throws {TypeError} first argument must be a function
1060+
* @returns {TypedArray} sorted array
1061+
*/
1062+
setReadOnly( TypedArray.prototype, 'sort', function sort( compareFcn ) {
1063+
var tmp;
1064+
var buf;
1065+
var len;
1066+
var i;
1067+
1068+
if ( !isTypedArray( this ) ) {
1069+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
1070+
}
1071+
tmp = [];
1072+
len = this._length;
1073+
buf = this._buffer;
1074+
for ( i = 0; i < len; i++ ) {
1075+
tmp.push( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) );
1076+
}
1077+
// If compare function is not provided
1078+
if ( arguments.length === 0 ) {
1079+
tmp.sort();
1080+
for ( i = 0; i < len; i++ ) {
1081+
buf[ SETTER ]( i * BYTES_PER_ELEMENT, tmp[ i ], this._isLE );
1082+
}
1083+
return this;
1084+
}
1085+
if ( !isFunction( compareFcn ) ) {
1086+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
1087+
}
1088+
1089+
tmp.sort( compareFcn );
1090+
for ( i = 0; i < len; i++ ) {
1091+
buf[ SETTER ]( i * BYTES_PER_ELEMENT, tmp[ i ], this._isLE );
1092+
}
1093+
1094+
return this;
1095+
});
1096+
11041097
/**
11051098
* Serializes an array as a string.
11061099
*

0 commit comments

Comments
 (0)