Skip to content

Add documentation for Array.prototype.toSorted method #3717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 1-js/05-data-types/05-array-methods/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,20 @@ alert( countries.sort( (a, b) => a.localeCompare(b) ) ); // Andorra,Österreich,
```
````

#### toSorted
[recent browser="new"]

The method [arr.toSorted](mdn:js/Array/toSorted) works similarly to the sort method, with one key difference:
It does not mutate the original array. Instead, it returns a new array that is sorted, leaving the original array unchanged.

For example:
```js run
const numbers = [3, 1, 4, 1, 5, 9];
const sortedNumbers = numbers.toSorted();
console.log(numbers); // [3, 1, 4, 1, 5, 9]
console.log(sortedNumbers); // [1, 1, 3, 4, 5, 9]
```

### reverse

The method [arr.reverse](mdn:js/Array/reverse) reverses the order of elements in `arr`.
Expand Down