• JavaScript Video Tutorials

JavaScript - Array .toSorted() Method



Sorting is a process of arranging items in a specific order such as numerical or alphabetical order. This gives a proper arrangement of the items and makes it easier to search for and find specific items in a list or array.

In JavaScript, the Array.toSorted() method is used to sort the elements of an array in a specific order (ascending or descending). It converts the elements into strings, then compares their sequences of UTF-16 code units values.

This method accepts an optional parameter: “compareFunction”, which defines the sorting order. If it is not provided, this method will sort in ascending order by default.

Syntax

Following is the syntax of JavaScript Array.toSorted() method −

toSorted(compareFn)

Parameters

This method accepts one optional parameter. The same is described below −

  • compareFn (optional) − This is a function that defines the sort order. If provided, the array will be first converted into strings and then sorted according to each character's unicode code point value.
    • a − The first element for comparison.
    • b − The second element for comparison.

Return value

This method returns a new array that contains elements sorted in ascending order.

Examples

Example 1

In the following example, we are sorting an array of numbers using the JavaScript Array.toSorted() method. As we haven't provided any function (that defines the sort order) as a parameter, it sorts in ascending order by default.

<html>
<body>
   <script>
      let numbers = [4, 2, 5, 1, 3];
      let result = numbers.toSorted();
      document.write(result);
   </script>
</body>
</html>

After executing the above program, it returns a new array that contains elements sorted in ascending order.

Output

1,2,3,4,5

Example 2

In this example, we are sorting the array of numbers in descending order by proving a comparision function as a parameter to the toSorted() method.

<html>
<body>
   <script>
      let numbers = [4, 2, 5, 1, 3];
      let result = numbers.toSorted((a, b) => b - a);
      document.write(result);
   </script>
</body>
</html>

If we execute the above program, we can see that the new array contains elements sorted in descending order.

Output

5,4,3,2,1

Example 3

In the example below, we are sorting an array of strings without passing a compare function to toSorted() method.

<html>
<body>
   <script>
      let fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
      let result = fruits.toSorted();
      document.write(result);
   </script>
</body>
</html>

As a result, the new array contains the string elements in ascending order.

Output

Apple,Banana,Mango,Orange

Example 4

In here, we are sorting an array of objects by a property in descending order.

<html>
<body>
   <script>
      let people = [
         { name: 'John', age: 25 },
         { name: 'Jane', age: 30 },
         { name: 'Alice', age: 20 },
      ];
      let result = people.toSorted((a, b) => b.age - a.age);
      document.write(JSON.stringify(result));
   </script>
</body>
</html>

If we execute the above program, it sorts the object by their age property in descending order.

Output

[{"name":"Jane","age":30},{"name":"John","age":25},{"name":"Alice","age":20}]
Advertisements