From d3c01c45920a6491feb8da42dff67bd40eb0598e Mon Sep 17 00:00:00 2001 From: Anzhela Date: Wed, 25 Feb 2026 20:58:52 +0200 Subject: [PATCH] solution --- src/arrayMethodSort.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..d5035832 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,39 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const array = this; + const length = array.length; + + const compare = + compareFunction || + function (a, b) { + const sA = String(a); + const sB = String(b); + + if (sA > sB) { + return 1; + } + + if (sA < sB) { + return -1; + } + + return 0; + }; + + for (let i = 0; i < length; i++) { + for (let j = 0; j < length - 1 - i; j++) { + if (compare(array[j], array[j + 1]) > 0) { + const temp = array[j]; + + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } + + return array; }; }