From dbcaae0140a8b79026f03ac060fbeafe578df36f Mon Sep 17 00:00:00 2001 From: Artem <139005232+CyborgNinjaHat@users.noreply.github.com> Date: Tue, 14 Apr 2026 11:39:08 +0000 Subject: [PATCH 1/3] feat: implement custom sort method --- src/arrayMethodSort.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..89e05753 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,20 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const compare = compareFunction + ? (a, b) => compareFunction(a, b) > 0 + : (a, b) => String(a) > String(b); + + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - i - 1; j++) { + if (compare(this[j], this[j + 1])) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + } + } + } + + return this; }; } From 7667085ddb8a49f8af5ae5cd642d43a4b609671b Mon Sep 17 00:00:00 2001 From: Artem <139005232+CyborgNinjaHat@users.noreply.github.com> Date: Tue, 14 Apr 2026 12:02:32 +0000 Subject: [PATCH 2/3] fix: add validation for compareFunction --- src/arrayMethodSort.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 89e05753..bdc095db 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -5,6 +5,13 @@ */ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { + const isInvalidCompareFunction = + compareFunction !== undefined && typeof compareFunction !== 'function'; + + if (isInvalidCompareFunction) { + throw new TypeError('compareFunction must be a function'); + } + const compare = compareFunction ? (a, b) => compareFunction(a, b) > 0 : (a, b) => String(a) > String(b); From 94e3fda5914dcba24b1287c43560fef31a5ca4f3 Mon Sep 17 00:00:00 2001 From: Artem <139005232+CyborgNinjaHat@users.noreply.github.com> Date: Wed, 15 Apr 2026 20:12:39 +0000 Subject: [PATCH 3/3] refactor: improve default comparison logic in custom sort method --- src/arrayMethodSort.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index bdc095db..e678a429 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -1,8 +1,20 @@ 'use strict'; -/** - * Implement method Sort - */ +function defaultCompare(a, b) { + const first = String(a); + const second = String(b); + + if (first < second) { + return -1; + } + + if (first > second) { + return 1; + } + + return 0; +} + function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { const isInvalidCompareFunction = @@ -12,13 +24,11 @@ function applyCustomSort() { throw new TypeError('compareFunction must be a function'); } - const compare = compareFunction - ? (a, b) => compareFunction(a, b) > 0 - : (a, b) => String(a) > String(b); + const compare = compareFunction ?? defaultCompare; for (let i = 0; i < this.length; i++) { for (let j = 0; j < this.length - i - 1; j++) { - if (compare(this[j], this[j + 1])) { + if (compare(this[j], this[j + 1]) > 0) { [this[j], this[j + 1]] = [this[j + 1], this[j]]; } }