-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Sort method #2499
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?
Sort method #2499
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,71 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| if ( | ||
| typeof compareFunction !== 'function' && | ||
| compareFunction !== undefined | ||
| ) { | ||
| throw new TypeError('Compare must be a function'); | ||
| } | ||
|
|
||
| const defaultCompare = (a, b) => { | ||
| const A = String(a); | ||
| const B = String(b); | ||
|
|
||
| if (A < B) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (A > B) { | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| }; | ||
|
|
||
| const comparator = | ||
| compareFunction === undefined ? defaultCompare : compareFunction; | ||
|
|
||
| const len = this.length >>> 0; | ||
| const items = []; | ||
|
|
||
| for (let i = 0; i < len; i++) { | ||
| if (i in this) { | ||
| items.push({ val: this[i], idx: i }); | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < items.length - 1; i++) { | ||
| for (let j = 0; j < items.length - 1 - i; j++) { | ||
|
Comment on lines
+42
to
+43
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. Bubble sort is used here which satisfies the task. As a non-blocking improvement for large arrays, consider implementing a more efficient sort (e.g., quicksort/mergesort) to reduce complexity. |
||
| const r = comparator(items[j].val, items[j + 1].val); | ||
| const shouldSwap = r === 0 ? items[j].idx > items[j + 1].idx : r > 0; | ||
|
|
||
| if (shouldSwap) { | ||
| const tmp = items[j]; | ||
|
|
||
| items[j] = items[j + 1]; | ||
| items[j + 1] = tmp; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let k = 0; | ||
|
|
||
| for (; k < items.length; k++) { | ||
| this[k] = items[k].val; | ||
| } | ||
|
|
||
| for (let i = k; i < len; i++) { | ||
| if (i in this) { | ||
| delete this[i]; | ||
| } | ||
| } | ||
|
|
||
| return this; | ||
| }; | ||
|
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 task requires implementing |
||
|
|
||
| [].__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.
Optional: the thrown TypeError message is fine functionally, but you might align it with the native message for clearer compatibility (this is not required by the task).