From ba12bfd6d2f06faf2b70f6ba3b2a466c368305cd Mon Sep 17 00:00:00 2001 From: Anna Pryimak Date: Fri, 10 Apr 2026 16:31:57 +0300 Subject: [PATCH 1/2] Solution --- src/arrayMethodSort.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..62aa6926 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,27 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const copy = [...this]; + let defaultFn = compareFunction; + + if (!compareFunction) { + defaultFn = (a, b) => String(a) > String(b); + } + + for (let i = 0; i < copy.length; i++) { + for (let j = 0; j < copy.length; j++) { + if (defaultFn(copy[j], copy[j + 1]) > 0) { + [copy[j], copy[j + 1]] = [copy[j + 1], copy[j]]; + } + } + } + + for (let i = 0; i < this.length; i++) { + this[i] = copy[i]; + } + + return this; }; } From b59eb0332abe5f715e61b4bdb48eef9860077383 Mon Sep 17 00:00:00 2001 From: Anna Pryimak Date: Fri, 24 Apr 2026 15:13:59 +0300 Subject: [PATCH 2/2] fixed --- src/arrayMethodSort.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 62aa6926..96f22e77 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -5,23 +5,35 @@ */ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { - const copy = [...this]; - let defaultFn = compareFunction; + const defaultCompareFunction = (a, b) => { + const strA = String(a); + const strB = String(b); - if (!compareFunction) { - defaultFn = (a, b) => String(a) > String(b); - } + if (strA < strB) { + return -1; + } - for (let i = 0; i < copy.length; i++) { - for (let j = 0; j < copy.length; j++) { - if (defaultFn(copy[j], copy[j + 1]) > 0) { - [copy[j], copy[j + 1]] = [copy[j + 1], copy[j]]; - } + if (strA > strB) { + return 1; } + + return 0; + }; + + let compare; + + if (typeof compareFunction === 'function') { + compare = compareFunction; + } else { + compare = defaultCompareFunction; } for (let i = 0; i < this.length; i++) { - this[i] = copy[i]; + for (let j = 0; j < this.length - 1; j++) { + if (compare(this[j], this[j + 1]) > 0) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + } + } } return this;