-
Notifications
You must be signed in to change notification settings - Fork 2.7k
completed the task #2457
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?
completed the task #2457
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,24 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| // const newArray = []; | ||
| let compair = compareFunction; | ||
|
|
||
| if (typeof compair !== 'function') { | ||
| compair = (a, b) => | ||
| String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0; | ||
| } | ||
|
|
||
| for (let i = 0; i < this.length - 1; i++) { | ||
|
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 implementation doesn't handle sparse arrays (arrays with empty slots) in the same way as the native |
||
| for (let j = i + 1; j < this.length; j++) { | ||
| if (compair(this[i], this[j]) > 0) { | ||
| [this[i], this[j]] = [this[j], this[i]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return this; | ||
| }; | ||
| } | ||
|
|
||
|
|
||
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 have a small typo in the variable name here. It should be
compareinstead ofcompair. This typo is used consistently throughout the function, so be sure to correct it in all places.