From db93816c1f8b00aa5b42443374ec396f52ad96bc Mon Sep 17 00:00:00 2001 From: Marian Date: Tue, 12 May 2026 03:10:47 +0300 Subject: [PATCH] add solution --- src/arrayMethodSort.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..2886ec54 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,35 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const arr = this; + + const compare = + compareFunction || + ((a, b) => { + if (String(a) > String(b)) { + return 1; + } + + if (String(a) < String(b)) { + return -1; + } + + return 0; + }); + + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr.length - 1 - i; j++) { + if (compare(arr[j], arr[j + 1]) > 0) { + const temp = arr[j]; + + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + return arr; }; }