From a797204d42d7c44c41f4dfddee9b59d30eb77a02 Mon Sep 17 00:00:00 2001 From: Olena Sobolieva Date: Wed, 4 Feb 2026 18:55:43 +0200 Subject: [PATCH 1/2] solution --- src/arrayMethodSort.js | 47 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..a0c903f4 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,53 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { + [].__proto__.sort2 = function (compareFunction) { // write code here + const length = this.length; + const array = this; + + let compare; + + if (typeof compareFunction === 'function') { + compare = compareFunction; + } else { + compare = (a, b) => { + const aStr = String(a); + const bStr = String(b); + + if (aStr < bStr) { + return -1; + } else if (aStr > bStr) { + return 1; + } else { + return 0; + } + }; + } + + for (let i = 0; i < length - 1; i++) { + for (let j = 0; j < length - i - 1; j++) { + const a = array[j]; + const b = array[j + 1]; + + if (a === undefined) { + continue; + } + + if (b === undefined) { + array[j] = b; + array[j + 1] = a; + continue; + } + + if (compare(a, b) > 0) { + array[j] = b; + array[j + 1] = a; + } + } + } + + return array; }; } From b6776b387572c0e7efe9428138ffd9c7394509d9 Mon Sep 17 00:00:00 2001 From: Olena Sobolieva Date: Wed, 4 Feb 2026 19:00:47 +0200 Subject: [PATCH 2/2] solution edited --- src/arrayMethodSort.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index a0c903f4..651a97fe 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -33,13 +33,17 @@ function applyCustomSort() { const a = array[j]; const b = array[j + 1]; - if (a === undefined) { + if (a === undefined && b !== undefined) { + array[j] = b; + array[j + 1] = a; continue; } - if (b === undefined) { - array[j] = b; - array[j + 1] = a; + if (a !== undefined && b === undefined) { + continue; + } + + if (a === undefined && b === undefined) { continue; }