• JavaScript Video Tutorials

JavaScript - TypedArray toSorted() Method



The JavaScript TypedArray toSorted() method is a copied version of the sort() method. It sorts the typed array elements in ascending order and returns a new typed array. It accepts an optional parameter.

The optional parameter is a function that accepts two parameters and defines the sorting order of the elements. You can define your own sorting order. For example: If it returns a > b, then the sorting order will be ascending. If it returns a < b, the sorting order will be descending.

Syntax

Following is the syntax of JavaScript TypedArray toSorted() method −

toSorted(compareFn)

Parameters

This method accepts a parameter named "compareFn", which is described below −

compareFn − A function that defines the sort order or typed array elements.

This function also accepts two parameters named 'a' and 'b'. Here's is description of each parameter −

  • a − The first value for the comparison.

  • b − The second value for the comparison.

Return value

This method returns a new typed array with elements sorted in ascending order.

Examples

Example 1

When the compareFn parameter is omitted, the toSorted() method of TypedArray instances sorts the elements numerically by default.

In the following example, we use the JavaScript TypedArray toSorted() method to create a new typed array with elements sorted in ascending order. We take an instance of this typed array: [15, 10, 20, 10, 5, 12, 4].

<html>
<head>
   <title>JavaScript TypedArray toSorted() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array( [15, 10, 20, 10, 5, 12, 4]);
      document.write("Typed array: ", T_array);
      const sorted_arr = T_array.toSorted();
      document.write("<br>Sorted(in ascending order) typed array: ", sorted_arr);
   </script>
</body>
</html>

Output

The above program returns the sorted typed array as −

Typed array: 15,10,20,10,5,12,4
Sorted(in ascending order) typed array: 4,5,10,10,12,15,20

Example 2

If we pass the compareFn as an argument to this method, it sorts the typed array elements based on the defined sort order of this compareFn function.

The following is another example of the JavaScript TypedArray toSorted() method. We create a function named descOrder() that defines the descending sort order of the elements, and then we pass this function as an argument to create a new typed array with elements sorted in descending order, taking an instance of this typed array [15, 10, 20, 10, 5, 12, 4].

<html>
<head>
   <title>JavaScript TypedArray toSorted() Method</title>
</head>
<body>
   <script>
      function desc(a, b){
         return a < b;
      }
      const T_array = new Uint8Array( [15, 10, 20, 10, 5, 12, 4]);
      document.write("Typed array: ", T_array);
      const sorted_arr = T_array.toSorted(desc);
      document.write("<br>Sorted(in descending order) typed array: ", sorted_arr);
   </script>
</body>
</html>

Output

After executing the above program, it will return a new typed array with elements in descending order as −

Typed array: 15,10,20,10,5,12,4
Sorted(in descending order) typed array: 20,15,12,10,10,5,4
Advertisements