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