diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..b2298162 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,25 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + for (let i = 0; i < this.length; ++i) { + for (let j = i + 1; j < this.length; ++j) { + let check = String(this[i]) > String(this[j]); + + if (compareFunction) { + check = compareFunction(this[i], this[j]) > 0; + } + + if (check) { + const temp = this[i]; + + this[i] = this[j]; + this[j] = temp; + } + } + } + + return this; }; }