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