From 9fdbd3b2f7f4d2372bf5b5d23b0618db67733a92 Mon Sep 17 00:00:00 2001 From: Erica Date: Sat, 25 Apr 2026 21:33:40 -0300 Subject: [PATCH 1/2] fix: methodSort --- src/arrayMethodSort.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..2c80ab8a 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,31 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + Array.prototype.sort2 = function (compareFunction) { + const arr = this; + + const defaultCompare = (a, b) => { + const strA = String(a); + const strB = String(b); + + if (strA > strB) return 1; + if (strA < strB) return -1; + return 0; + }; + + const compare = compareFunction || defaultCompare; + + for (let i = 0; i < arr.length - 1; i++) { + for (let j = 0; j < arr.length - 1 - i; j++) { + if (compare(arr[j], arr[j + 1]) > 0) { + const temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + return arr; }; } From 7979bbcd6e0bbf9841169bb60958788df8aefca0 Mon Sep 17 00:00:00 2001 From: Erica Date: Sat, 25 Apr 2026 21:38:19 -0300 Subject: [PATCH 2/2] feat: implement custom sort2 --- src/arrayMethodSort.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 2c80ab8a..9e379ff3 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -1,8 +1,7 @@ 'use strict'; -/** - * Implement method Sort - */ +/* eslint-disable no-extend-native */ + function applyCustomSort() { Array.prototype.sort2 = function (compareFunction) { const arr = this;