JavaScript Array sort() method

The JavaScript array sort() method is used to arrange the array elements in some order. By default, sort() method follows the ascending order.

Syntax

The sort() method is represented by the following syntax:

Parameter

compareFunction - It is optional. It represents a function that provides an alternative sort order.

Return

An array of sorted elements

JavaScript Array sort() method example

Here, we will understand sort() method through various examples.

Example 1

Let's see a simple example to sort the array of string elements.

Test it Now

Output:

AngularJS,Bootstrap,JQuery,Node.js

Example 2

Let's see a simple example to sort the array of integer elements.

Test it Now

Output:

1,2,4,5,8

Example 3

Let's see an example to arrange the elements in ascending order using function.

Test it Now

Output:

1,2,4,5,8

Example 4

Let's see an example to arrange the elements in descending order using function.

Test it Now

Output:

8,5,4,2,1

Example 5

Let's see an example to extract minimum value from an array.

Test it Now

Output:

1

Example 6

Let's see an example to extract maximum value from an array.

Test it Now

Output:

8





Latest Courses