From 51faf2d520ca20b0c11935e3add7138806b45fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssia?= Date: Sun, 12 Apr 2026 22:38:19 -0300 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..6b692738 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,46 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + let compare = compareFunction; + + if (typeof compare !== 'function') { + compare = function (a, b) { + const aString = String(a); + const bString = String(b); + + if (aString < bString) { + return -1; + } + + if (aString > bString) { + return 1; + } + + return 0; + }; + } + + const arr = this.slice(); + const sortedArr = []; + + while (arr.length > 0) { + let minIndex = 0; + + for (let i = 1; i < arr.length; i++) { + if (compare(arr[i], arr[minIndex]) < 0) { + minIndex = i; + } + } + sortedArr.push(arr[minIndex]); + arr.splice(minIndex, 1); + } + + for (let i = 0; i < sortedArr.length; i++) { + this[i] = sortedArr[i]; + } + + return this; }; }