-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 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,8 +4,48 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| // const newArray = []; | ||
| let compare = compareFunction; | ||
| const result = []; | ||
| const undefinedNumber = []; | ||
|
|
||
| if (typeof compare !== 'function') { | ||
| compare = (a, b) => | ||
| String(a) > String(b) ? 1 : String(a) < String(b) ? -1 : 0; | ||
| } | ||
|
|
||
| for (let i = 0; i < this.length - 1; i++) { | ||
| for (let j = i + 1; j < this.length; j++) { | ||
| if (this[i] === undefined) { | ||
| continue; | ||
| } | ||
|
|
||
| if (this[j] === undefined) { | ||
| continue; | ||
| } | ||
|
Comment on lines
+20
to
+26
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 logic for handling |
||
|
|
||
| if (compare(this[i], this[j]) > 0) { | ||
| [this[i], this[j]] = [this[j], this[i]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < this.length; i++) { | ||
| if (this[i] !== undefined) { | ||
| result[result.length] = this[i]; | ||
| } else { | ||
| undefinedNumber[undefinedNumber.length] = this[i]; | ||
| } | ||
| } | ||
|
Comment on lines
+34
to
+40
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 block of code correctly separates defined values from undefined/empty ones. However, it treats an empty slot in a sparse array the same as an element that is explicitly To fix this, you need to distinguish between these two cases. You can check if an index actually exists in the array using the |
||
|
|
||
| const sorted = [...result, ...undefinedNumber]; | ||
|
|
||
| for (let i = 0; i < sorted.length; i++) { | ||
| this[i] = sorted[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.
This implementation doesn't handle sparse arrays (arrays with empty slots) in the same way as the native
Array.prototype.sort. The native method moves all empty slots to the end of the array, whereas your current logic treats them asundefinedand includes them in the sorting comparison based on the string 'undefined'.