From 891bae2861043f86b0de4ae13fee0c3bb1b2ab99 Mon Sep 17 00:00:00 2001 From: Serhii Shimko Date: Sat, 31 Jan 2026 12:51:50 +0200 Subject: [PATCH 1/3] completed the task --- src/arrayMethodSort.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..e1b5bef2 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,24 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + // const newArray = []; + let compair = compareFunction; + + if (typeof compair !== 'function') { + compair = (a, b) => + String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0; + } + + for (let i = 0; i < this.length - 1; i++) { + for (let j = i + 1; j < this.length; j++) { + if (compair(this[i], this[j]) > 0) { + [this[i], this[j]] = [this[j], this[i]]; + } + } + } + + return this; }; } From 9a5207f99ad949ed1b90d011014571f147d1344c Mon Sep 17 00:00:00 2001 From: Serhii Shimko Date: Sat, 31 Jan 2026 13:21:54 +0200 Subject: [PATCH 2/3] completed the task --- src/arrayMethodSort.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index e1b5bef2..3afc7f3c 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -6,21 +6,40 @@ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { // const newArray = []; - let compair = compareFunction; + let compare = compareFunction; + const result = []; - if (typeof compair !== 'function') { - compair = (a, b) => + if (typeof compare !== 'function') { + compare = (a, b) => String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0; } for (let i = 0; i < this.length - 1; i++) { for (let j = i + 1; j < this.length; j++) { - if (compair(this[i], this[j]) > 0) { + if (this[i] === undefined) { + continue; + } + + if (this[j] === undefined) { + continue; + } + + if (compare(this[i], this[j]) > 0) { [this[i], this[j]] = [this[j], this[i]]; } } } + for (let i = 0; i < this.length - 1; i++) { + if (this[i] !== undefined) { + result[result.length] = this[i]; + } + } + + for (let i = 0; i < result.length - 1; i++) { + this[i] = result[i]; + } + return this; }; } From 27d17d057907b23bec0ebc6e82a98c2cee0219a0 Mon Sep 17 00:00:00 2001 From: Serhii Shimko Date: Sat, 31 Jan 2026 13:35:37 +0200 Subject: [PATCH 3/3] completed the task --- src/arrayMethodSort.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 3afc7f3c..d04391e8 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -8,6 +8,7 @@ function applyCustomSort() { // const newArray = []; let compare = compareFunction; const result = []; + const undefinedNumber = []; if (typeof compare !== 'function') { compare = (a, b) => @@ -30,14 +31,18 @@ function applyCustomSort() { } } - for (let i = 0; i < this.length - 1; i++) { + for (let i = 0; i < this.length; i++) { if (this[i] !== undefined) { result[result.length] = this[i]; + } else { + undefinedNumber[undefinedNumber.length] = this[i]; } } - for (let i = 0; i < result.length - 1; i++) { - this[i] = result[i]; + const sorted = [...result, ...undefinedNumber]; + + for (let i = 0; i < sorted.length; i++) { + this[i] = sorted[i]; } return this;