From 350c8d9ec0f8f5ac9b01e4e7a7edac27ce415baa Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 23 Feb 2026 19:49:51 +0200 Subject: [PATCH 1/3] Solution --- src/arrayMethodSort.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..3f5a123a 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,36 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const cmp = + compareFunction || + function (a, b) { + const strA = String(a); + const strB = String(b); + + if (strA < strB) { + return -1; + } + + if (strA > strB) { + return 1; + } + + return 0; + }; + + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - i - 1; j++) { + if (cmp(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; } From 70575d959f59413ff55a0c091faf25d1caba8f9f Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 23 Feb 2026 20:24:06 +0200 Subject: [PATCH 2/3] made corrections --- src/arrayMethodSort.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 3f5a123a..53c606b4 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -35,6 +35,10 @@ function applyCustomSort() { return this; }; + + return function (arr, compareFn) { + return arr.sort2(compareFn); + }; } module.exports = applyCustomSort; From f45e7ec54cc4b39c854e51968e686c76d62cb180 Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 23 Feb 2026 20:43:20 +0200 Subject: [PATCH 3/3] fixed the mistakes --- src/arrayMethodSort.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 53c606b4..9968d6a6 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -36,9 +36,11 @@ function applyCustomSort() { return this; }; - return function (arr, compareFn) { - return arr.sort2(compareFn); + /* eslint-disable no-extend-native */ + Array.prototype.sort = function (compareFn) { + return this.sort2(compareFn); }; + /* eslint-enable no-extend-native */ } module.exports = applyCustomSort;