From 621c0b48d70906cfe2c05b466325da768bd13df8 Mon Sep 17 00:00:00 2001 From: Myroslav Holub Date: Tue, 14 Apr 2026 17:08:55 +0300 Subject: [PATCH 1/3] first solution --- src/arrayMethodSort.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..bfdd755d 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,19 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const callback = + compareFunction || ((a, b) => (String(a) > String(b) ? 1 : -1)); + + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - i - 1; j++) { + if (callback(this[j], this[j + 1]) > 0) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + } + } + } + + return this; }; } From a3fda36f5ee6de00026e7f5945df78a93e1ccd17 Mon Sep 17 00:00:00 2001 From: Myroslav Holub Date: Tue, 14 Apr 2026 17:15:20 +0300 Subject: [PATCH 2/3] second solution --- src/arrayMethodSort.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index bfdd755d..e03e2f94 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -6,7 +6,21 @@ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { const callback = - compareFunction || ((a, b) => (String(a) > String(b) ? 1 : -1)); + 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 - 1; i++) { for (let j = 0; j < this.length - i - 1; j++) { From 446134f1bcf6a0eacd2a1d82d49d3c490d89ddc7 Mon Sep 17 00:00:00 2001 From: Myroslav Holub Date: Tue, 14 Apr 2026 21:54:03 +0300 Subject: [PATCH 3/3] final commit --- src/arrayMethodSort.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index e03e2f94..8fa9e21c 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -32,6 +32,10 @@ function applyCustomSort() { return this; }; + + [].__proto__.sort = function (compareFunction) { + return this.sort2(compareFunction); + }; } module.exports = applyCustomSort;