From b3f7bfba238226d647c3d6c4bcf40f6964d1d4a0 Mon Sep 17 00:00:00 2001 From: Rostyslav Date: Mon, 23 Feb 2026 09:13:11 +0200 Subject: [PATCH 1/3] Solution --- src/arrayMethodSort.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..5689de87 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,24 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + for (let i = 0; i < this.length; i++) { + for (let o = 0; o < this.length - 1 - i; o++) { + let shouldSwap; + + if (typeof compareFunction === 'function') { + shouldSwap = compareFunction(this[o], this[o + 1]) > 0; + } else { + shouldSwap = String(this[o]) > String(this[o + 1]); + } + + if (shouldSwap) { + [this[o], this[o + 1]] = [this[o + 1], this[o]]; + } + } + } + + return this; }; } From f12032c7c6ade4694fd55b14797806d7d8ea3bdc Mon Sep 17 00:00:00 2001 From: Rostyslav Date: Mon, 23 Feb 2026 09:31:34 +0200 Subject: [PATCH 2/3] Solution --- src/arrayMethodSort.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 5689de87..93eaa2d6 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -5,6 +5,15 @@ */ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { + if ( + compareFunction !== undefined && + typeof compareFunction !== 'function' + ) { + throw new TypeError( + 'The comparison function must be either a function or undefined', + ); + } + for (let i = 0; i < this.length; i++) { for (let o = 0; o < this.length - 1 - i; o++) { let shouldSwap; From 8716f117e2afd98a997c884d0111e3c110faf725 Mon Sep 17 00:00:00 2001 From: Rostyslav Date: Mon, 23 Feb 2026 09:49:04 +0200 Subject: [PATCH 3/3] Solution --- src/arrayMethodSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 93eaa2d6..2499f478 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -32,6 +32,8 @@ function applyCustomSort() { return this; }; + /* eslint-disable-next-line no-extend-native */ + Array.prototype.sort = [].__proto__.sort2; } module.exports = applyCustomSort;