From 204af335a4fa258bffcdcfed1cd7f855c2b989fa Mon Sep 17 00:00:00 2001 From: mohammadkamraan Date: Mon, 15 Jul 2024 22:15:13 +0330 Subject: [PATCH] chore: add the toSorted array method to Array methods section --- 1-js/05-data-types/05-array-methods/article.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/1-js/05-data-types/05-array-methods/article.md b/1-js/05-data-types/05-array-methods/article.md index 8536459582..c357389c0d 100644 --- a/1-js/05-data-types/05-array-methods/article.md +++ b/1-js/05-data-types/05-array-methods/article.md @@ -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`.