From d333901558a14d504f52faaeac0060e7cd1a3c5f Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 4 May 2026 22:50:18 +0200 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..bdcbff61 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,9 +4,40 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + function compareAsString(a, b) { + const aStr = String(a); + const bStr = String(b); + + if (aStr > bStr) { + return 1; + } + + if (aStr < bStr) { + return -1; + } + + return 0; + } + + [].__proto__.sort2 = function (compareFunction = compareAsString) { + let count; + + do { + count = 0; + + for (let i = 0; i < this.length - 1; i++) { + const a = this[i]; + const b = this[i + 1]; + + if (compareFunction(a, b) > 0) { + count++; + this[i] = b; + this[i + 1] = a; + } + } + } while (count > 0); + + return this; }; } - module.exports = applyCustomSort;