From 507acf62364594b74c8985dc51890d335e6f317e Mon Sep 17 00:00:00 2001 From: diachkinainna Date: Wed, 4 Feb 2026 15:22:13 +0200 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..1eeda0a2 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,32 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const comparisor = + compareFunction || + function (item1, item2) { + return String(item1) > String(item2) ? 1 : -1; + }; + + while (true) { + let flag = true; + + for (let i = 0; i < this.length - 1; i++) { + if (comparisor(this[i], this[i + 1]) > 0) { + const helper = this[i]; + + this[i] = this[i + 1]; + this[i + 1] = helper; + flag = false; + } + } + + if (flag) { + break; + } + } + + return this; }; }