From d001b7cf0aa82f8da28e03c6ab3d0fbe58da9a12 Mon Sep 17 00:00:00 2001 From: linska Date: Thu, 9 Apr 2026 15:29:06 +0300 Subject: [PATCH] feat: implement sort function --- src/arrayMethodSort.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..b2298162 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,25 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + for (let i = 0; i < this.length; ++i) { + for (let j = i + 1; j < this.length; ++j) { + let check = String(this[i]) > String(this[j]); + + if (compareFunction) { + check = compareFunction(this[i], this[j]) > 0; + } + + if (check) { + const temp = this[i]; + + this[i] = this[j]; + this[j] = temp; + } + } + } + + return this; }; }