-
Notifications
You must be signed in to change notification settings - Fork 2.7k
first solution #2503
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?
first solution #2503
Changes from 1 commit
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,8 +4,19 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| const callback = | ||
| compareFunction || ((a, b) => (String(a) > String(b) ? 1 : -1)); | ||
|
|
||
| for (let i = 0; i < this.length - 1; i++) { | ||
| for (let j = 0; j < this.length - i - 1; j++) { | ||
| if (callback(this[j], this[j + 1]) > 0) { | ||
| [this[j], this[j + 1]] = [this[j + 1], this[j]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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 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. After you define
|
||
| }; | ||
| } | ||
|
|
||
|
|
||
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.
The default comparator used here returns 1 when
a > band -1 otherwise, which also returns -1 whenaandbare equal. Per the usualsortcontract, the comparator should return 0 when values are equal. Consider adjusting to return 0 for equality, for example by checking<and>explicitly and returning 0 when neither is true.