-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Solution #2473
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?
Solution #2473
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 |
|---|---|---|
|
|
@@ -4,9 +4,43 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| const cmp = | ||
| compareFunction || | ||
|
Comment on lines
+8
to
+9
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. As an optional improvement, you could add a check here to ensure that if |
||
| function (a, b) { | ||
| const strA = String(a); | ||
| const strB = String(b); | ||
|
|
||
| if (strA < strB) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (strA > strB) { | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| }; | ||
|
|
||
| for (let i = 0; i < this.length - 1; i++) { | ||
| for (let j = 0; j < this.length - i - 1; j++) { | ||
| if (cmp(this[j], this[j + 1]) > 0) { | ||
| const temp = this[j]; | ||
|
|
||
| this[j] = this[j + 1]; | ||
| this[j + 1] = temp; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+25
to
+34
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. This bubble sort implementation works correctly. For future reference, if performance were a critical concern, more efficient sorting algorithms like QuickSort or MergeSort would be preferred, as bubble sort has a time complexity of O(n^2). However, for this task, it's perfectly acceptable. |
||
|
|
||
| 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. Before returning from Array.prototype.sort = function(compareFn) {
return this.sort2(compareFn);
};This is required by the task description (implement |
||
| }; | ||
|
|
||
|
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. Missing override of |
||
| /* eslint-disable no-extend-native */ | ||
| Array.prototype.sort = function (compareFn) { | ||
| return this.sort2(compareFn); | ||
| }; | ||
| /* eslint-enable no-extend-native */ | ||
| } | ||
|
|
||
| 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: validate
compareFunctiontype. IfcompareFunctionis provided but is not a function, consider throwing aTypeErrorto better mirror nativeArray.prototype.sortbehavior (not strictly required by tests but improves compatibility).