From d31a3d28922101e438acdcc5cba6bd6859f406ce Mon Sep 17 00:00:00 2001 From: Sofiia Hryshko Date: Wed, 20 May 2026 17:43:55 +0300 Subject: [PATCH] solution --- src/arrayMethodSort.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..8719816f 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,37 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFn) { + const compare = + compareFn ?? + ((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 = 1; i < this.length; i++) { + const current = this[i]; + let j = i - 1; + + while (j >= 0 && compare(this[j], current) > 0) { + this[j + 1] = this[j]; + j--; + } + + this[j + 1] = current; + } + + return this; }; }