From 84cc0b496d9c221e6c2cfe26f715a44b3270019c Mon Sep 17 00:00:00 2001 From: Svitlana Slavets Date: Mon, 16 Mar 2026 21:33:02 +0100 Subject: [PATCH 1/3] Solution --- src/arrayMethodSort.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..526643d7 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,21 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function ( + compareFunction = (a, b) => String(a) > String(b), + ) { + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - 1; j++) { + if (compareFunction(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; } From 4ffd8eaa7be8c1d0f0d51e446d3536c9bc61836b Mon Sep 17 00:00:00 2001 From: Svitlana Slavets Date: Thu, 26 Mar 2026 22:41:09 +0100 Subject: [PATCH 2/3] Solution --- src/arrayMethodSort.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 526643d7..f0c1d512 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,12 +4,16 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function ( - compareFunction = (a, b) => String(a) > String(b), - ) { + [].__proto__.sort2 = function (compareFunction) { + let cmp = compareFunction; + + if (typeof cmp !== 'function') { + cmp = (a, b) => `${a}` > `${b}`; + } + for (let i = 0; i < this.length - 1; i++) { - for (let j = 0; j < this.length - 1; j++) { - if (compareFunction(this[j], this[j + 1]) > 0) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (cmp(this[j], this[j + 1]) > 0) { const temp = this[j]; this[j] = this[j + 1]; From e9b31bae8fc72983ad5d9407ed98a92e455479f3 Mon Sep 17 00:00:00 2001 From: Svitlana Slavets Date: Thu, 26 Mar 2026 23:34:08 +0100 Subject: [PATCH 3/3] Solution --- src/arrayMethodSort.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index f0c1d512..528cdf5d 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -8,7 +8,8 @@ function applyCustomSort() { let cmp = compareFunction; if (typeof cmp !== 'function') { - cmp = (a, b) => `${a}` > `${b}`; + cmp = (a, b) => + String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0; } for (let i = 0; i < this.length - 1; i++) {