From b28b565c2513baae04cf598107fe5f7de6d5f92c Mon Sep 17 00:00:00 2001 From: Ihor Haidai Date: Tue, 3 Mar 2026 18:37:30 -0500 Subject: [PATCH] Add task solution --- src/arrayMethodSort.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..63cb3729 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,38 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + // Default comparator (same as native sort) + const cmp = + compareFunction || + function (a, b) { + const A = String(a); + const B = String(b); + + if (A < B) { + return -1; + } + + if (A > B) { + return 1; + } + + return 0; + }; + + // Simple bubble sort (in-place) + for (let i = 0; i < this.length - 1; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (cmp(this[j], this[j + 1]) > 0) { + const temp = this[j]; + + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + } + + return this; // must return original array }; }