JavaScript Array sort()

Definition and Usage

sort() The method sorts the items in the array.

The sorting order can be alphabetical or numerical, as well as ascending (up) or descending (down).

By default,sort() The method will sort values as strings in alphabetical and ascending order.

This applies to strings ("Apple" appears before "Banana"). However, if numbers are sorted as strings, "25" is greater than "100" because "2" is greater than "1".

Therefore,sort() The method may produce incorrect results when sorting numbers.

You can solve this problem by providing a 'comparison function' (see the 'Parameter Values' below).

Note:sort() The method will change the original array.

Instance

Example 1

Sort the array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();

Try It Yourself

Example 2

Sort numbers in ascending order in the array:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});

Try It Yourself

Example 3

Sort numbers in descending order in the array:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});

Try It Yourself

Example 4

Get the minimum value in the array:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});    // Sort the numbers in the array in ascending order
// The first item (points[0]) in the array is now the lowest value

Try It Yourself

Example 5

Get the maximum value in the array:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});    // Sort the numbers in the array in descending order
// The first item (points[0]) in the array is now the highest value

Try It Yourself

Example 6

Get the maximum value in the array:

const points = [40, 100, 1, 5, 25, 10];
// Sort the numbers in ascending order:
points.sort(function(a, b){return a-b});
// points[points.length-1] = 100 (highest value)

Try It Yourself

Example 7

Sort the array in alphabetical order and then reverse the order of the sorted items (descending):

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();

Try It Yourself

Syntax

array.sort(compareFunction)

Parameter Values

Parameters Description
compareFunction

Optional. Defines a function to replace the sorting order. This function should return a negative value, zero value, or positive value depending on the parameters, for example:

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

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

Example:

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

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

The sort function will sort 40 as a value less than 100.

Technical Details

Return Value: Array object, with items sorted.
JavaScript Version: ECMAScript 1

Browser Support

All browsers fully support sort() Methods:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related Pages

Tutorial:JavaScript Array

Tutorial:JavaScript Array Const

Tutorial:JavaScript Array Methods

Tutorial:JavaScript Array Sorting

Tutorial:JavaScript Array Iteration

Manual:JavaScript Array reverse() Method