Skip to content

Sort method#2499

Open
balakhmei wants to merge 2 commits intomate-academy:masterfrom
balakhmei:develop
Open

Sort method#2499
balakhmei wants to merge 2 commits intomate-academy:masterfrom
balakhmei:develop

Conversation

@balakhmei
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

I am requesting changes because the main task requirement—overriding Array.prototype.sort to use your custom sort2 implementation—is not yet fulfilled, which will cause tests that call [].sort() to fail. In src/arrayMethodSort.js, you correctly define [].__proto__.sort2 and handle comparator validation, default string comparison, stability, and sparse arrays, but you never actually assign this function to Array.prototype.sort. To fix this, add an assignment near the end of the file, right after the sort2 function definition (e.g., around the last lines), such as:

Array.prototype.sort = Array.prototype.sort2; // or [].__proto__.sort2

This ensures that all normal calls like myArray.sort() use your custom logic, satisfying the core requirement. As a minor, non-blocking improvement, you might align your TypeError message with the native one and consider a more efficient algorithm than bubble sort for very large arrays. Your implementation is otherwise thoughtful and careful about edge cases, so with this small but crucial wiring change, it should fully meet the task expectations.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/arrayMethodSort.js
}

return this;
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires implementing Array.prototype.sort using the predefined [].__proto__.sort2, but this file only defines sort2 and never assigns it to the built-in sort. Consider adding an assignment such as Array.prototype.sort = Array.prototype.sort2; (or Array.prototype.sort = [].__proto__.sort2;) after this line so the task requirement is satisfied.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Your solution is approved because it correctly implements a custom Array.prototype.sort using your sort2 function and meets the functional requirements of the task. The implementation properly validates the comparator, supplies a default string-based comparison when none is provided, maintains stability by tracking original indices, and correctly handles sparse arrays, so normal arr.sort() calls should work as expected. As a small, optional improvement, you could adjust the TypeError message thrown when the comparator is invalid (in src/arrayMethodSort.js, where you check the argument type) to more closely match the native message, which can help with compatibility and debugging. Another non-blocking suggestion is to consider a more efficient algorithm than bubble sort (such as mergesort or quicksort) if you ever adapt this for very large arrays, since bubble sort is O(n^2) and can become slow at scale, even though it fully satisfies the current assignment. Overall, you’ve addressed the earlier missing-prototype-override issue well, and the code demonstrates a solid understanding of custom sorting behavior—nice work.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment thread src/arrayMethodSort.js
typeof compareFunction !== 'function' &&
compareFunction !== undefined
) {
throw new TypeError('Compare must be a function');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: the thrown TypeError message is fine functionally, but you might align it with the native message for clearer compatibility (this is not required by the task).

Comment thread src/arrayMethodSort.js
Comment on lines +42 to +43
for (let i = 0; i < items.length - 1; i++) {
for (let j = 0; j < items.length - 1 - i; j++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bubble sort is used here which satisfies the task. As a non-blocking improvement for large arrays, consider implementing a more efficient sort (e.g., quicksort/mergesort) to reduce complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants