From 4e42448c5850b8c82d287524c6215d96e1a33bb7 Mon Sep 17 00:00:00 2001 From: Nataliia Date: Thu, 21 May 2026 17:23:15 +0200 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..05349b08 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,20 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const compare = + compareFunction || + ((a, b) => (String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0)); + + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - i - 1; j++) { + if (compare(this[j], this[j + 1]) > 0) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + } + } + } + + return this; }; }