From c611972ca0595f6d10b9da2b8053286e62f75d80 Mon Sep 17 00:00:00 2001 From: Jonereh Date: Tue, 24 Feb 2026 22:54:09 +0200 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..5900e643 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,38 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + let callback = compareFunction; + + if (!compareFunction) { + callback = (item1, item2) => { + const strItem1 = String(item1); + const strItem2 = String(item2); + + if (strItem1 > strItem2) { + return 1; + } + + if (strItem1 < strItem2) { + return -1; + } + + return 0; + }; + } + + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1; j++) { + if (callback(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; }