From e9895ea645966222a251461553efabf8dfb1c278 Mon Sep 17 00:00:00 2001 From: Thiago Moro do Carmo Date: Thu, 19 Mar 2026 14:05:49 -0300 Subject: [PATCH 1/2] solution --- src/arrayMethodSort.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..a164bb3d 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,33 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + let temporaryValue; + let hasChanged = true; + + while (hasChanged === true) { + hasChanged = false; + + for (let i = 1; i < this.length; i++) { + if (typeof compareFunction === 'function') { + if (compareFunction(this[i - 1], this[i]) > 0) { + temporaryValue = this[i - 1]; + this[i - 1] = this[i]; + this[i] = temporaryValue; + hasChanged = true; + } + } else { + if (this[i - 1].toString() > this[i].toString()) { + temporaryValue = this[i - 1]; + this[i - 1] = this[i]; + this[i] = temporaryValue; + hasChanged = true; + } + } + } + } + + return this; }; } From db8795de4fea55fcd1f200d9483312b8bb8bc341 Mon Sep 17 00:00:00 2001 From: Thiago Moro do Carmo Date: Thu, 19 Mar 2026 14:22:59 -0300 Subject: [PATCH 2/2] change toString() to String() --- src/arrayMethodSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index a164bb3d..65166048 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -20,7 +20,7 @@ function applyCustomSort() { hasChanged = true; } } else { - if (this[i - 1].toString() > this[i].toString()) { + if (String(this[i - 1]) > String(this[i])) { temporaryValue = this[i - 1]; this[i - 1] = this[i]; this[i] = temporaryValue;