From 939f56d23c9520f949a6243efeb9167d62667635 Mon Sep 17 00:00:00 2001 From: Tetiana Date: Fri, 23 Jan 2026 18:33:01 +0100 Subject: [PATCH 1/3] solution --- src/arrayMethodSort.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..214f5196 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,22 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const compare = + compareFunction || ((a, b) => String(a).localeCompare(String(b))); + + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - 1; j++) { + if (compare(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; } From 105357bd4640223e0930c9e17af80eeed37c2191 Mon Sep 17 00:00:00 2001 From: Tetiana Date: Sat, 24 Jan 2026 12:43:19 +0100 Subject: [PATCH 2/3] solution --- src/arrayMethodSort.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 214f5196..495c9153 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,17 +4,16 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function (compareFunction) { - const compare = - compareFunction || ((a, b) => String(a).localeCompare(String(b))); + const compareByDefault = (a, b) => (String(a) > String(b) ? 1 : -1); - for (let i = 0; i < this.length - 1; i++) { - for (let j = 0; j < this.length - 1; j++) { - if (compare(this[j], this[j + 1]) > 0) { - const temp = this[j]; + [].__proto__.sort2 = function (callback = compareByDefault) { + for (let j = 0; j < this.length; j++) { + for (let i = 0; i < this.length; i++) { + if (callback(this[i], this[i + 1]) > 0) { + const temp = this[i]; - this[j] = this[j + 1]; - this[j + 1] = temp; + this[i] = this[i + 1]; + this[i + 1] = temp; } } } From 4a002c2000d876d8ca50108aa794169db45ce3d2 Mon Sep 17 00:00:00 2001 From: Tetiana Date: Sat, 24 Jan 2026 12:48:16 +0100 Subject: [PATCH 3/3] Solution --- src/arrayMethodSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 495c9153..35e42acc 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -8,7 +8,7 @@ function applyCustomSort() { [].__proto__.sort2 = function (callback = compareByDefault) { for (let j = 0; j < this.length; j++) { - for (let i = 0; i < this.length; i++) { + for (let i = 0; i < this.length - 1; i++) { if (callback(this[i], this[i + 1]) > 0) { const temp = this[i];