Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,43 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {
// const newArray = [];
let compare = compareFunction;
const result = [];

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++) {
Copy link
Copy Markdown

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 as undefined and includes them in the sorting comparison based on the string 'undefined'.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic for handling undefined values doesn't correctly implement the behavior for sparse arrays. The native sort method moves all empty slots (which are different from undefined values) to the end. Simply skipping these elements leaves them in their relative positions instead of moving them. Consider modifying your comparison logic to treat empty slots as being 'greater' than any defined value, which will cause them to be sorted to the end.


if (compare(this[i], this[j]) > 0) {
[this[i], this[j]] = [this[j], this[i]];
}
}
}

for (let i = 0; i < this.length - 1; i++) {
if (this[i] !== undefined) {
result[result.length] = this[i];
}
}
Comment on lines +34 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 undefined. This causes empty slots to be converted to undefined values in the final sorted array, which differs from the native sort behavior.

To fix this, you need to distinguish between these two cases. You can check if an index actually exists in the array using the in operator (e.g., i in this). This will be true for an index with an undefined value but false for an empty slot.


for (let i = 0; i < result.length - 1; i++) {
this[i] = result[i];
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code intended to reconstruct the array has a couple of issues. The loop conditions, such as i < this.length - 1, will cause the last element of the array to be skipped in both loops. A more direct approach would be to handle the empty slots within the main sorting loop, which would make this extra reconstruction step unnecessary.


return this;
};
}

Expand Down