From 31d04e955c01b9bf8b4ed2d865d87825a39cb905 Mon Sep 17 00:00:00 2001 From: Tendeeer Date: Thu, 28 May 2026 22:48:17 +0300 Subject: [PATCH] sort method --- src/arrayMethodSort.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..b262d252 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,9 +4,32 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const arr = this; + + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr.length - i - 1; j++) { + let result; + + if (typeof compareFunction === 'function') { + result = compareFunction(arr[j], arr[j + 1]); + } else { + const a = String(arr[j]); + const b = String(arr[j + 1]); + + result = a > b ? 1 : a < b ? -1 : 0; + } + + if (result > 0) { + const temp = arr[j]; + + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + return this; }; } - module.exports = applyCustomSort;