Skip to content

Solution#2459

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

Solution#2459
flyman13 wants to merge 2 commits intomate-academy:masterfrom
flyman13:develop

Conversation

@flyman13
Copy link

@flyman13 flyman13 commented Feb 3, 2026

No description provided.

Copy link

@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 implementing the custom sort2 method and handling the logic for both default and custom comparison functions. Your approach is on the right track!

However, this requires changes because the current implementation doesn't handle undefined values correctly. The native Array.prototype.sort moves all undefined elements to the end of the array without passing them to the compareFunction. Your code passes undefined to the comparator, which can lead to incorrect results, such as NaN when using a numeric comparison like (a, b) => a - b.

Please update your sorting logic to specifically handle undefined values by moving them to the end of the array, just as the native method does.


✨ 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
const shouldSwap = compareFunction
? compareFunction(arr[j], arr[j + 1]) > 0
: String(arr[j]) > String(arr[j + 1]);

Choose a reason for hiding this comment

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

This logic doesn't handle undefined values correctly when a compareFunction is provided. The native Array.prototype.sort moves all undefined elements to the end of the array without passing them to the compareFunction. Your current implementation passes undefined to compareFunction, which can lead to unexpected behavior (e.g., (a, b) => a - b would result in NaN).

Consider adding logic to handle undefined values specifically, treating them as if they are larger than any other value.

Copy link

@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 iteration! I am approving your solution as you've successfully addressed all the previous feedback. The logic for handling undefined values is now correct: they are moved to the end of the array and are not passed to the compareFunction, which perfectly meets the task requirements. For future learning, you might consider exploring more efficient sorting algorithms like Quicksort or Merge Sort, but your current implementation is a great solution for this problem. Well done!


✨ 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