diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..02a1035f 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -1,11 +1,24 @@ 'use strict'; -/** - * Implement method Sort - */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const compare = + compareFunction || + ((a, b) => (String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0)); + + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1; j++) { + if (compare(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + + this[j + 1] = temp; + } + } + } + + return this; }; }