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