diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..8719816f 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,37 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFn) { + const compare = + compareFn ?? + ((a, b) => { + const strA = String(a); + const strB = String(b); + + if (strA < strB) { + return -1; + } + + if (strA > strB) { + return 1; + } + + return 0; + }); + + for (let i = 1; i < this.length; i++) { + const current = this[i]; + let j = i - 1; + + while (j >= 0 && compare(this[j], current) > 0) { + this[j + 1] = this[j]; + j--; + } + + this[j + 1] = current; + } + + return this; }; }