Skip to content

Solution#2483

Open
s-slavets wants to merge 3 commits intomate-academy:masterfrom
s-slavets:develop
Open

Solution#2483
s-slavets wants to merge 3 commits intomate-academy:masterfrom
s-slavets:develop

Conversation

@s-slavets
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

Great job setting up the sort2 method on the Array prototype! However, this requires a change to meet the core requirements. The main issue is that your default compareFunction does not correctly replicate the native sort() behavior. It returns a boolean, but a comparison function must return a number (negative, zero, or positive) to indicate the sort order. This causes arrays like [10, 1] to be sorted incorrectly. To fix this, the default function should compare elements as strings and return a numeric value. You might find String.prototype.localeCompare() helpful here. Once you've updated the comparison logic, I'll be happy to take another look!


✨ 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

[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (
compareFunction = (a, b) => String(a) > String(b),
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 default compareFunction is not correctly implemented. It should return a number (negative, 0, or positive) to be compliant with the Array.prototype.sort specification, but it returns a boolean. For example, sorting [10, 1] will not work as expected because '10' > '1' evaluates to false. A correct implementation could use String.prototype.localeCompare or explicit checks to return -1, 0, or 1.

compareFunction = (a, b) => String(a) > String(b),
) {
for (let i = 0; i < this.length - 1; i++) {
for (let j = 0; j < this.length - 1; 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.

This bubble sort implementation can be slightly optimized. After each full pass of the outer loop, the largest element for that pass is moved to its correct sorted position at the end of the unsorted portion of the array. Therefore, the inner loop doesn't need to check the elements that are already sorted. You can adjust the inner loop's condition to j < this.length - 1 - i.

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

Great work on implementing the custom sort method! You're very close to a complete solution. The main issue is that your default compareFunction returns a boolean (true/false) instead of a number.

The native Array.prototype.sort specification requires the comparator to return a negative number, zero, or a positive number to determine the sort order. While your current logic works due to type coercion, it doesn't correctly replicate the required behavior.

Please update the function to return a numeric value. For the default string comparison, String(a).localeCompare(String(b)) is an excellent way to achieve this. After that one change, this will be perfect!


✨ 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

let cmp = compareFunction;

if (typeof cmp !== 'function') {
cmp = (a, b) => `${a}` > `${b}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great progress on fixing the sorting logic! However, the compareFunction is still returning a boolean (true/false) instead of a number as required by the sort method's specification. A proper compare function must return a positive number, a negative number, or zero. While this works in your if statement due to type coercion, it doesn't fully replicate the native sort behavior. Please adjust this to return a numeric value, for example by using String.prototype.localeCompare().

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

Excellent work on this task! I am approving your solution. You've done a fantastic job of addressing the previous feedback by updating the default compareFunction to return a numeric value (-1, 0, or 1). This change correctly aligns your implementation with the behavior of the native sort method and fulfills all the requirements. As a suggestion for your continued learning, you could explore more efficient sorting algorithms like Merge Sort or Quick Sort. For this task, however, your implementation is perfect. Keep up the great work!


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

Footnotes

  1. Rate AI review example

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