From 9e7ac52b61cd61d097fa3f53d3ec011b0ffc805c Mon Sep 17 00:00:00 2001 From: nataliandr Date: Wed, 29 Apr 2026 14:35:18 +0200 Subject: [PATCH 1/2] add arrayMethodSort Solution --- src/arrayMethodSort.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..6d9dd64e 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,42 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const defaultCompare = (a, b) => { + const sA = String(a); + const sB = String(b); + + if (sA < sB) { + return -1; + } + + if (sA > sB) { + return 1; + } + + return 0; + }; + + const compare = compareFunction || defaultCompare; + + // 2. Алгоритм Bubble Sort + // Робимо копію довжини, щоб не звертатися до this.length постійно + const len = this.length; + + for (let i = 0; i < len; i++) { + for (let j = 0; j < len - 1 - i; j++) { + // Викликаємо compare з поточними елементами + if (compare(this[j], this[j + 1]) > 0) { + // Міняємо місцями (Swap) + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; } From b12bafb4928ddbae725285fcb24143e596010850 Mon Sep 17 00:00:00 2001 From: nataliandr Date: Wed, 29 Apr 2026 14:39:38 +0200 Subject: [PATCH 2/2] add arrayMethodSort Solution --- src/arrayMethodSort.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 6d9dd64e..27c22ebe 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -22,15 +22,11 @@ function applyCustomSort() { const compare = compareFunction || defaultCompare; - // 2. Алгоритм Bubble Sort - // Робимо копію довжини, щоб не звертатися до this.length постійно const len = this.length; for (let i = 0; i < len; i++) { for (let j = 0; j < len - 1 - i; j++) { - // Викликаємо compare з поточними елементами if (compare(this[j], this[j + 1]) > 0) { - // Міняємо місцями (Swap) const temp = this[j]; this[j] = this[j + 1];