Skip to content
27 changes: 25 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
'use strict';
"use strict";

/**
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
[].__proto__.sort2 = function (compareFunction) {
// write code here
const callback =
typeof compareFunction === "function"
? compareFunction
: (a, b) =>
String(a).localeCompare(String(b), undefined, {

Check failure on line 13 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12
sensitivity: "case",

Check failure on line 14 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 12 spaces but found 14
});

Check failure on line 15 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12

for (let i = this.length - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (callback(this[j], this[i]) > 0) {
swap(this, i, j);
}
}
}

return this;
};
}

function swap(arr, firstElementIndex, secondElementIndex) {
const tempPlaceHolder = arr[firstElementIndex];
arr[firstElementIndex] = arr[secondElementIndex];
arr[secondElementIndex] = tempPlaceHolder;
}

module.exports = applyCustomSort;
Loading