-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Task done! Array metodo sort #2466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,52 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| if ( | ||
| compareFunction !== undefined && | ||
| typeof compareFunction !== 'function' | ||
| ) { | ||
| throw new TypeError( | ||
| 'The comparison function must be either a function or undefined', | ||
| ); | ||
|
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thrown TypeError message text is currently: |
||
| } | ||
|
|
||
| let swapped; | ||
|
|
||
| do { | ||
| swapped = false; | ||
|
|
||
|
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional suggestion: add a short comment that this implementation uses bubble sort (O(n²)) and that more efficient algorithms (e.g. quicksort/mergesort) are preferable for large arrays. This is informational only and not required by the task. |
||
| for (let i = 0; i < this.length - 1; i++) { | ||
| const a = this[i]; | ||
| const b = this[i + 1]; | ||
| let shouldSwap = false; | ||
|
|
||
| if (compareFunction) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| if (compareFunction(a, b) > 0) { | ||
| shouldSwap = true; | ||
| } | ||
| } else { | ||
|
Comment on lines
+27
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You use |
||
| if (String(a) > String(b)) { | ||
| shouldSwap = true; | ||
| } | ||
| } | ||
|
|
||
| if (shouldSwap) { | ||
| const temp = this[i]; | ||
|
|
||
| this[i] = this[i + 1]; | ||
|
|
||
| this[i + 1] = temp; | ||
|
|
||
| swapped = true; | ||
| } | ||
| } | ||
| } while (swapped); | ||
|
|
||
| return this; | ||
| }; | ||
|
|
||
| [].__proto__.sort = [].__proto__.sort2; | ||
| } | ||
|
|
||
| module.exports = applyCustomSort; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You define
[].__proto__.sort2here, but the task requires implementingArray.prototype.sortusing the predefinedsort2. After definingsort2, assign it to the standardsortso calls toarr.sort()use your implementation (for example, setArray.prototype.sortto thesort2implementation).