diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..1eeda0a2 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,32 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const comparisor = + compareFunction || + function (item1, item2) { + return String(item1) > String(item2) ? 1 : -1; + }; + + while (true) { + let flag = true; + + for (let i = 0; i < this.length - 1; i++) { + if (comparisor(this[i], this[i + 1]) > 0) { + const helper = this[i]; + + this[i] = this[i + 1]; + this[i + 1] = helper; + flag = false; + } + } + + if (flag) { + break; + } + } + + return this; }; }