From 717f744223d15c525aafdff33ede6ceeb5af4378 Mon Sep 17 00:00:00 2001 From: Oleh Bunchak Date: Wed, 29 Apr 2026 13:57:00 +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..458abdbb 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 A = String(a); + const B = String(b); + + if (A > B) { + return 1; + } + + if (A < B) { + return -1; + } + + return 0; + }); + + for (let i = 0; i < this.length; i++) { + for (let j = i + 1; j < this.length; j++) { + if (compare(this[i], this[j]) > 0) { + const temp = this[i]; + + this[i] = this[j]; + this[j] = temp; + } + } + } + + return this; }; }