From 2af9635d3ee42ad8294f6d8ab80c8fa84c0c9a39 Mon Sep 17 00:00:00 2001 From: Sidach Ruslan Date: Mon, 30 Mar 2026 09:24:41 +0300 Subject: [PATCH] add solution --- src/arrayMethodSort.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..dfa0ded5 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,33 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + for (let i = 1; i < this.length; i++) { + const currentElement = this[i]; + let correctIndex = i; + + for (let j = i - 1; j >= 0; j--) { + let insertIndex; + + if (compareFunction === undefined) { + insertIndex = String(this[j]) > String(currentElement); + } else { + insertIndex = compareFunction(this[j], currentElement) > 0; + } + + if (!insertIndex) { + break; + } + + // shift array + this[j + 1] = this[j]; + correctIndex = j; + } + + this[correctIndex] = currentElement; + } + + return this; }; }