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