Skip to content

Solution#2490

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

Solution#2490
Oleksandr-zg wants to merge 2 commits intomate-academy:masterfrom
Oleksandr-zg:develop

Conversation

@Oleksandr-zg
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 work on the overall structure of your custom sort method, especially how it handles a provided compareFunction. However, I'm requesting a change because the default sorting logic doesn't align with the native Array.prototype.sort behavior, which is a core requirement.

The issue is the special handling for numbers. When no compareFunction is given, the native sort() method converts all elements to strings and compares them lexicographically. For example, [1, 10, 2].sort() results in [1, 10, 2] because the string '10' comes before '2'. Your implementation sorts this numerically to [1, 2, 10], which is incorrect for the default case.

To fix this, please remove the special if block for numbers and let your existing string comparison logic (lines 18-24) handle all data types when no compareFunction is passed.


✨ 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 on lines +14 to +16
if (typeof a === 'number' && typeof b === 'number') {
return String(a).localeCompare(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 sort() method converts elements to strings and compares them lexicographically. This special handling for numbers using localeCompare deviates from the native behavior. For example, [1, 10, 2].sort() results in [1, 10, 2], because '10' comes before '2' in string comparison. Your logic on lines 18-24 already correctly implements this string-based comparison, which should be applied to all types when no compareFunction is 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

Excellent work on this task! I am approving your solution as you've successfully addressed the previous feedback and met all the requirements. Your implementation of the default sorting logic now correctly converts elements to strings for lexicographical comparison, perfectly mimicking the native sort() method's behavior. The code is clean and functions as expected. 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

Comment on lines +26 to +32
for (let i = 0; i < this.length; i++) {
for (let j = i + 1; j < this.length; j++) {
if (compares(this[i], this[j]) > 0) {
[this[i], this[j]] = [this[j], this[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.

Your implementation using bubble sort is correct for this task. As a suggestion for future learning, you could explore more efficient sorting algorithms like Merge Sort or Quick Sort. They have a better average time complexity (O(n log n)) compared to bubble sort (O(n^2)), which makes a big difference for large arrays.

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