From ee9e4ca86479502b6592385f5d1b4cf8361b0bbf Mon Sep 17 00:00:00 2001 From: svladimir1010 Date: Thu, 28 May 2026 21:12:51 +0300 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..4d8ca577 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,29 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + if (this.length === 0) { + return this; + } + + const defaulCompare = (a, b) => { + return String(a) === String(b) ? 0 : String(a) > String(b) ? 1 : -1; + }; + + const compare = compareFunction || defaulCompare; + + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (compare(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; }