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