From 8591bf4e1d8720ef9ee52776a929d4281de23537 Mon Sep 17 00:00:00 2001 From: Olga Bilokur Date: Sat, 4 Apr 2026 23:21:43 +0300 Subject: [PATCH] Solution --- src/arrayMethodSort.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..5d0f85ff 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,37 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { + [].__proto__.sort2 = function (compareFunction) { // write code here + const compare = + compareFunction || + ((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 < this.length - 1; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (compare(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; }; }