JavaScript Array toSorted()

Definition and usage

toSorted() The method sorts the array elements in alphabetical order.

toSorted() The method returns a new array.

toSorted() The method does not overwrite the original array.

toSorted() The method is sort() A copy version of the method.

See also:

Array sort() method

Array reverse() method

Sorting comparison function

Sorting strings in alphabetical order works well ("Apple" comes before "Banana").

However, sorting numbers may produce incorrect results.

"25" is greater than "100" because "2" is greater than "1".

This problem can be solved by providing a "comparison function" (see the example below).

Instance

Example 1

// Create an array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Sort the array
const fruits2 = fruits.toSorted();

Try it yourself

Example 2

Reversed order after sorting:

// Create an array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Sort the array
const fruits2 = fruits.toSorted();
// Reverse the array
fruits2.reverse();

Try it yourself

Example 3

Use the sorting function to sort numbers in ascending order:

// Create an array
const points = [40, 100, 1, 5, 25, 10];
// Sort the array
let points2 = points.toSorted(function(a, b){return a - b});

Try it yourself

Example 4

Sort numbers in descending order:

// Create an array
const points = [40, 100, 1, 5, 25, 10];
// Sort the array
let points2 = points.toSorted(function(a, b){return b - a});

Try it yourself

Example 5

Find the minimum value:

// Create an array
const points = [40, 100, 1, 5, 25, 10];
// Sort numbers in ascending order
let points2 = points.toSorted(function(a, b){return a - b});
let lowest = points2[0];

Try it yourself

Example 6

Find the maximum value:

// Create an array
const points = [40, 100, 1, 5, 25, 10];
// Sort numbers in descending order
let points2 = points.toSorted(function(a, b){return b - a});
let highest = points2[0];

Try it yourself

Example 7

Find the maximum value:

// Create an array
const points = [40, 100, 1, 5, 25, 10];
// Sort numbers in ascending order
let points2 = points.toSorted(function(a, b){return a - b});
let highest = points2[points.length - 1];

Try it yourself

Syntax

array.sort(compareFunction)

Parameters

Parameters Description
compareFunction

Optional. Defines the function to define the sorting order. This function should return a negative value, zero, or a positive value based on the parameters:

function(a, b){return a-b}

When sort() compares two values, it sends these values to the comparison function and sorts the values based on the returned (negative, zero, positive) values.

Example:

The sorting function considers 40 to be a smaller value than 100.

When comparing 40 and 100, the sort() method calls the function function(40, 100).

The function calculates 40 - 100 and returns -60 (a negative value).

Return Value

Type Description
Array New array sorted.

Browser Support

toSorted() It is a feature of ES2023.

Starting from July 2023, all modern browsers support this method:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023