From e0effc22fb39646c4cfa054fce3c3664a27909b3 Mon Sep 17 00:00:00 2001 From: Taras Peteichuk Date: Sat, 7 Feb 2026 19:52:01 +0200 Subject: [PATCH 1/2] added task solution --- src/arrayMethodSort.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..dde57625 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,28 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + if (typeof compareFunction !== 'function') { + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (String(this[j]) > String(this[j + 1])) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + } + } + } + + return this; + } + + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (compareFunction(this[j], this[j + 1]) > 0) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + } + } + } + + return this; }; } From 39c3e76dca8f90f5b0fea64202ff2f590435bac3 Mon Sep 17 00:00:00 2001 From: Taras Peteichuk Date: Sat, 7 Feb 2026 20:13:57 +0200 Subject: [PATCH 2/2] removed duplicated code --- src/arrayMethodSort.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index dde57625..3c24491f 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -5,21 +5,11 @@ */ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { - if (typeof compareFunction !== 'function') { - for (let i = 0; i < this.length - 1; i++) { - for (let j = 0; j < this.length - 1 - i; j++) { - if (String(this[j]) > String(this[j + 1])) { - [this[j], this[j + 1]] = [this[j + 1], this[j]]; - } - } - } - - return this; - } + let defaultCompare = compareFunction || ((a, b) => String(a) > String(b)); for (let i = 0; i < this.length - 1; i++) { for (let j = 0; j < this.length - 1 - i; j++) { - if (compareFunction(this[j], this[j + 1]) > 0) { + if (defaultCompare(this[j], this[j + 1]) > 0) { [this[j], this[j + 1]] = [this[j + 1], this[j]]; } }