⚡ Sangsakala Proxy 🏠 Home
Skip to content

feat: add toSorted() explanation and examples. - #3963

Open
AlexNT-maker wants to merge 2 commits into
javascript-tutorial:masterfrom
AlexNT-maker:patch-1
Open

AlexNT-maker wants to merge 2 commits into
javascript-tutorial:masterfrom
AlexNT-maker:patch-1

Conversation

@AlexNT-maker

Copy link
Copy Markdown
  • Briefly explains the concept of immutability and why mutating original data is avoided in modern JS.
  • Introduce toSorted() as the modern alternative to sort() .
  • Includes a runnable code block (js run) demonstrating how the original array remains untouched compared to the new sorted array.

- Briefly explains the concept of immutability and why mutating original data is avoided in modern JS.
- Introduce` toSorted()` as the modern alternative to `sort()` .
- Includes a runnable code block (`js run`) demonstrating how the original array remains untouched compared to the new sorted array.
@javascript-translate-bot javascript-translate-bot added the review needed Review needed, please approve or request changes label May 21, 2026
@javascript-translate-bot
javascript-translate-bot requested review from a team May 21, 2026 13:18
@CLAassistant

CLAassistant commented May 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@AKzar1el AKzar1el left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One accuracy issue in the new explanation, noted inline.


### toSorted

In modern JavaScript, we generally avoid mutating original data, as it can lead to unexpected side effects and bugs. Because the traditional `sort()` method modifies the array in place, a new modern method was introduced. The `toSorted()` method elegantly solves this problem by returning a completely new, sorted array, leaving the initial data safely intact. It is also more optimized than the older workaround of manually cloning the array before sorting.

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 claim that toSorted() is “more optimized” than copying and sorting isn't guaranteed. MDN describes it as the copying version of sort(), whose complexity depends on the implementation; readers may mistake this for a performance guarantee over [...arr].sort(). Could we remove the performance claim and keep the clear non-mutating behavior explanation?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done! Thanks for the review! I have removed the performance claim in the latest commit.

Removed mention of manual cloning in toSorted method explanation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review needed Review needed, please approve or request changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants