From 0d4bab7416f0de2bec90da32ce2ed22d1cb7e2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=A7=D0=B5=D1=80=D0=BD?= =?UTF-8?q?=D0=B8=D1=88=D0=BE=D0=B2=D0=B0?= Date: Sat, 21 Mar 2026 12:38:33 +0200 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..2a1b7070 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,39 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { + [].__proto__.sort2 = function (compareFunction) { // write code here + const arr = this; + + const compare = + compareFunction || + function (a, b) { + const strA = String(a); + const strB = String(b); + + if (strA > strB) { + return 1; + } + + if (strA < strB) { + 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; }; }