JavaScript Tutorials

JavaScript Array toSorted()
Syntax & Examples

Array.toSorted() method

The toSorted() method of the Array class in JavaScript returns a new array with the elements sorted in ascending order, without modifying the original array.


Syntax of Array.toSorted()

There are 2 variations for the syntax of Array.toSorted() method. They are:

1.
toSorted()

This method returns a new array with the elements sorted in ascending, UTF-16 code unit order.

Returns value of type Array.

2.
toSorted(compareFn)

Parameters

ParameterOptional/RequiredDescription
compareFnoptionalA function that defines the sort order. It takes two arguments: a and b. If compareFn(a, b) is less than 0, a comes before b. If compareFn(a, b) is greater than 0, a comes after b. If compareFn(a, b) is equal to 0, the order remains unchanged.

This method returns a new array with the elements sorted according to the compare function.

Returns value of type Array.



✐ Examples

1 Using toSorted() method to sort an array of numbers in ascending order

In JavaScript, we can use the toSorted() method to sort an array of numbers in ascending order without modifying the original array.

For example,

  1. We define an array variable arr with elements [5, 3, 8, 1, 2].
  2. We use the toSorted() method on arr to sort the elements in ascending order.
  3. The result is stored in the variable sortedArr.
  4. We log sortedArr and the original array to the console using console.log() method to see the sorted array and confirm the original array is unchanged.

JavaScript Program

const arr = [5, 3, 8, 1, 2];
const sortedArr = arr.toSorted();
console.log(sortedArr);  // Output: [1, 2, 3, 5, 8]
console.log(arr);        // Output: [5, 3, 8, 1, 2]

Output

[1, 2, 3, 5, 8]
[5, 3, 8, 1, 2]

2 Using toSorted() method to sort an array of strings in alphabetical order

We can use the toSorted() method to sort an array of strings in alphabetical order without modifying the original array.

For example,

  1. We define an array variable strArr with elements ['banana', 'apple', 'cherry', 'date'].
  2. We use the toSorted() method on strArr to sort the elements in alphabetical order.
  3. The result is stored in the variable sortedStrArr.
  4. We log sortedStrArr and the original array to the console using console.log() method to see the sorted array and confirm the original array is unchanged.

JavaScript Program

const strArr = ['banana', 'apple', 'cherry', 'date'];
const sortedStrArr = strArr.toSorted();
console.log(sortedStrArr);  // Output: ['apple', 'banana', 'cherry', 'date']
console.log(strArr);        // Output: ['banana', 'apple', 'cherry', 'date']

Output

['apple', 'banana', 'cherry', 'date']
['banana', 'apple', 'cherry', 'date']

3 Using toSorted() method with a compare function to sort an array of numbers in descending order

We can use the toSorted() method with a compare function to sort an array of numbers in descending order without modifying the original array.

For example,

  1. We define an array variable arr with elements [5, 3, 8, 1, 2].
  2. We define a compare function compareNumbers that sorts the numbers in descending order.
  3. We use the toSorted() method on arr with compareNumbers to sort the elements in descending order.
  4. The result is stored in the variable sortedArr.
  5. We log sortedArr and the original array to the console using console.log() method to see the sorted array and confirm the original array is unchanged.

JavaScript Program

const arr = [5, 3, 8, 1, 2];
const compareNumbers = (a, b) => b - a;
const sortedArr = arr.toSorted(compareNumbers);
console.log(sortedArr);  // Output: [8, 5, 3, 2, 1]
console.log(arr);        // Output: [5, 3, 8, 1, 2]

Output

[8, 5, 3, 2, 1]
[5, 3, 8, 1, 2]

Summary

In this JavaScript tutorial, we learned about toSorted() method of Array: the syntax and few working examples with output and detailed explanation for each example.