From f08f62c7a307d4c2e102204540d47f52a264092a Mon Sep 17 00:00:00 2001 From: darvexon Date: Tue, 14 Apr 2026 16:55:42 +0300 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..a731a62f 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,9 +4,37 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction = basicCompare) { + for (let i = 0; i < this.length - 1; i++) { + const elementToCompare1 = this[i]; + const elementToCompare2 = this[i + 1]; + const callback = compareFunction(elementToCompare1, elementToCompare2); + + if (callback > 0) { + this[i] = elementToCompare2; + this[i + 1] = elementToCompare1; + + i = -1; + } + } + + return this; }; + + function basicCompare(a, b) { + const aLower = String(a).toLowerCase(); + const bLower = String(b).toLowerCase(); + + if (aLower !== String(a) && bLower === String(b)) { + return -1; + } + + if (bLower !== String(b) && aLower === String(a)) { + return 1; + } + + return String(a).localeCompare(String(b)); + } } module.exports = applyCustomSort;