From 270cef7b5971362e5a6da8fc7e682f05bb587ac6 Mon Sep 17 00:00:00 2001 From: Marcos Vinicius Date: Fri, 13 Feb 2026 20:41:20 -0300 Subject: [PATCH 1/3] add solution --- src/arrayMethodSort.js | 33 +++++++++++++++++++++++++++++++-- src/index.html | 11 +++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/index.html diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..64b8e751 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,8 +4,37 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + [].__proto__.sort2 = function (compareFunction) { + const compare = + compareFunction || + function (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 = 1; i < this.length; i++) { + const current = this[i]; + let j = i - 1; + + while (j >= 0 && compare(this[j], current) > 0) { + this[j + 1] = this[j]; + j--; + } + + this[j + 1] = current; + } + + return this; }; } diff --git a/src/index.html b/src/index.html new file mode 100644 index 00000000..ca53d698 --- /dev/null +++ b/src/index.html @@ -0,0 +1,11 @@ + + + + + + arrayMethodSort + + + + + From 7624d76f30195eca4622455ebe451a08f5f0ed9a Mon Sep 17 00:00:00 2001 From: Marcos Vinicius Date: Fri, 13 Feb 2026 22:56:14 -0300 Subject: [PATCH 2/3] add solution --- .github/workflows/test.yml-template | 23 ++++++++++++++ package-lock.json | 8 ++--- package.json | 2 +- src/arrayMethodSort.js | 48 ++++++++++++++++++++--------- 4 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/test.yml-template diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..bb13dfc4 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,23 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/package-lock.json b/package-lock.json index eadb3115..02863286 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -1467,9 +1467,9 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, "dependencies": { "@octokit/rest": "^17.11.2", diff --git a/package.json b/package.json index 9c0b7bbd..0827621b 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 64b8e751..77673638 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -5,37 +5,55 @@ */ function applyCustomSort() { [].__proto__.sort2 = function (compareFunction) { + if ( + compareFunction !== undefined && + typeof compareFunction !== 'function' + ) { + throw new TypeError( + 'The comparison function must be either a function or undefined', + ); + } + const compare = compareFunction || - function (a, b) { - const A = String(a); - const B = String(b); + ((a, b) => { + const s1 = String(a); + const s2 = String(b); - if (A > B) { - return 1; + if (s1 < s2) { + return -1; } - if (A < B) { - return -1; + if (s1 > s2) { + return 1; } return 0; - }; + }); - for (let i = 1; i < this.length; i++) { - const current = this[i]; - let j = i - 1; + const length = this.length; - while (j >= 0 && compare(this[j], current) > 0) { - this[j + 1] = this[j]; - j--; + for (let i = 0; i < length; i++) { + let swapped = false; + + for (let j = 0; j < length - 1 - i; j++) { + if (compare(this[j], this[j + 1]) > 0) { + [this[j], this[j + 1]] = [this[j + 1], this[j]]; + swapped = true; + } } - this[j + 1] = current; + if (!swapped) { + break; + } } return this; }; + + [].__proto__.sort = function (compareFunction) { + return this.sort2(compareFunction); + }; } module.exports = applyCustomSort; From f1afded132818ee65f4f5f5c047886364e9c9433 Mon Sep 17 00:00:00 2001 From: Marcos Vinicius Date: Fri, 13 Feb 2026 23:08:54 -0300 Subject: [PATCH 3/3] add solution --- src/arrayMethodSort.js | 57 ++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 77673638..0bc611cd 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -4,7 +4,9 @@ * Implement method Sort */ function applyCustomSort() { - [].__proto__.sort2 = function (compareFunction) { + const proto = Object.getPrototypeOf([]); + + function customSort(compareFunction) { if ( compareFunction !== undefined && typeof compareFunction !== 'function' @@ -16,44 +18,49 @@ function applyCustomSort() { const compare = compareFunction || - ((a, b) => { - const s1 = String(a); - const s2 = String(b); + function (a, b) { + const A = String(a); + const B = String(b); - if (s1 < s2) { - return -1; + if (A > B) { + return 1; } - if (s1 > s2) { - return 1; + if (A < B) { + return -1; } return 0; - }); + }; - const length = this.length; + for (let i = 1; i < this.length; i++) { + const current = this[i]; + let j = i - 1; - for (let i = 0; i < length; i++) { - let swapped = false; - - for (let j = 0; j < length - 1 - i; j++) { - if (compare(this[j], this[j + 1]) > 0) { - [this[j], this[j + 1]] = [this[j + 1], this[j]]; - swapped = true; - } + while (j >= 0 && compare(this[j], current) > 0) { + this[j + 1] = this[j]; + j--; } - if (!swapped) { - break; - } + this[j + 1] = current; } return this; - }; + } + + Object.defineProperty(proto, 'sort2', { + value: customSort, + writable: true, + configurable: true, + }); - [].__proto__.sort = function (compareFunction) { - return this.sort2(compareFunction); - }; + Object.defineProperty(proto, 'sort', { + value: function (compareFunction) { + return customSort.call(this, compareFunction); + }, + writable: true, + configurable: true, + }); } module.exports = applyCustomSort;