From beb0836788f7afedd8f56919077a0d32447fa113 Mon Sep 17 00:00:00 2001 From: blabunch Date: Wed, 18 Feb 2026 17:43:18 +0200 Subject: [PATCH 1/2] solution --- src/arrayMethodSort.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..1e8e2146 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -5,7 +5,24 @@ */ function applyCustomSort() { [].__proto__.sort2 = function(compareFunction) { - // write code here + const compare = compareFunction || function (a, b) { + const sA = String(a); + const sB = String(b); + if (sA < sB) return -1; + if (sA > sB) return 1; + return 0; + } + + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1 - i; 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 e92fd7de592aaa91b07b2cad3b1e947a22664763 Mon Sep 17 00:00:00 2001 From: blabunch Date: Wed, 18 Feb 2026 17:50:54 +0200 Subject: [PATCH 2/2] solution --- src/arrayMethodSort.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 1e8e2146..4b53f05f 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,7 +4,12 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { + [].__proto__.sort2 = function (compareFunction) { + + if (compareFunction !== undefined && typeof compareFunction !== 'function') { + throw new TypeError('The comparison function must be either a function or undefined'); + } + const compare = compareFunction || function (a, b) { const sA = String(a); const sB = String(b); @@ -24,6 +29,10 @@ function applyCustomSort() { } return this; }; + + [].__proto__.sort = function(compareFunction) { + return this.sort2(compareFunction); + }; } module.exports = applyCustomSort;