How toSorted() method is different from sort() in JavaScript Last Updated : 05 Dec, 2023 Comments Improve Suggest changes Like Article Like Report In JavaScript, sort() and toSorted() methods do the same job as sorting but toSorted() does not change the original array. sort() method changes the original array. Below we have explained both the methods. Table of Content Sort() MethodtoSorted() MethodSort() Method:The sort() method is a built-in method for arrays in JavaScript that directly modifies the array in place.By default, it sorts the elements as strings, so for numeric sorting, a comparison function needs to be provided.Syntax:arr.sort()Example: In this example, We have used sort() method to sort an array. Here, we can see that sort() method change the original array. JavaScript let arr = [3,4,1,2,8,2]; let arr2 = arr.sort(); console.log("arr = ",arr); console.log("arr2 = ",arr2); Output:arr = [ 1, 2, 2, 3, 4, 8 ]arr2 = [ 1, 2, 2, 3, 4, 8 ]toSorted() Method:The toSorted() method of array instances is the copying version of the sort() method. It returns a new array with the elements sorted in ascending order and original array won't get affected. Syntax:let arr2 = arr.toSorted()Example: In this example, We have used toSorted() method. Here we can see that toSorted() method does not change the original array. JavaScript let arr = [3,4,1,2,8,2]; let arr2 = arr.toSorted(); console.log("arr ="arr); console.log("arr2 = "arr2); Output:arr = [3, 4, 1, 2, 8, 2]arr2 = [1, 2, 2, 3, 4, 8] Comment More infoAdvertise with us Next Article How toSorted() method is different from sort() in JavaScript ukasp Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How toReversed() method is different from reverse() method in JavaScript In JavaScript, reverse() and toReversed() methods do the same job as reversing but toReversed() does not change the original array. The reverse() method changes the original array. Below we have explained both methods. Table of Content Reverse() MethodtoReversed() methodReverse() MethodThe reverse() 2 min read How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s 3 min read JavaScript Array toSorted() Method The JS arr.toSorted() method rearranges the array elements alphabetically and returns a new sorted array.Sorting Array of StringsJavaScriptconst arr = ["JS", "HTML", "CSS"]; const arr1 = arr.toSorted(); console.log(arr); console.log(arr1);Output[ 'JS', 'HTML', 'CSS' ][ 'CSS', 'HTML', 'JS' ]Array.toS 3 min read How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach 3 min read How to sort a collection in JavaScript ? A javascript collection is much like a container. It's just an item that combines several elements into a single unit. Aggregate information is stored, accessed, modified, and communicated via collections. With the help of constructors, we create collections in javascript. In earlier versions of jav 3 min read How are elements ordered in a Map in JavaScript ? In JavaScript, a new object called Map was introduced in the ES6 version. A map is a collection of elements where each element is stored in a key, value pair. Map objects can store both objects as well as primitive data types. The elements of a map are iterable. Elements are always iterated in the i 2 min read How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort 3 min read What is the difference between unshift() and Push() method in JavaScript? JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array. Javascript Array unshift() method: The JavaScript Array unshift() Method 2 min read JavaScript Object entries() Method The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed.Syntax:Object.entries(obj);Parameters:obj 4 min read JavaScript Array sort() Method The JS array.sort() method rearranges the array elements alphabetically and returns a sorted array. It does not create a new array but updates the original array.Sorting Array of StringsJavaScript// Original array let ar = ["JS", "HTML", "CSS"]; console.log(ar); // Sorting the array ar.sort() consol 4 min read Like