• JavaScript Video Tutorials

JavaScript - Array sort() Method



The JavaScript Array.sort() function is used to sort the elements of an array. By default, this method converts array elements into strings and then sorts them based on their UTF-16 code unit values.

This method accepts a single parameter: “compareFunction”, which overrides the default sorting behavior. The compare function is used to determine the sorting order of the elements (can be either ascending or descending order).

If you want to sort the elements in an array without modifying the original array, use JavaScript Array.toSorted() method.

Syntax

Following is the syntax of JavaScript Array sort() method −

array.sort(CompareFunction);

Parameters

This method accepts only one parameter, which is described below −

CompareFunction − This is a function that defines the sort order of the array elements. This function takes two parameters (A and B) representing two elements in the array for comparison.

  • If (A – B) < 0, a negative value, then A comes first.
  • If (A – B) = 0, then nothing will be changed.
  • If (A – B) > 0, a positive value, then B comes first.

Return value

This method sorts the elements of an array in place and returns the sorted array.

Examples

Example 1

In the following example, we haven’t passed any parameter to the JavaScript Array sort() function. So, the sort() method convert elements to strings and then compare their sequences of UTF-16 code units and returns the result.

<html>
<body>
   <p id="demo"></p>
   <script>
      const array = [10, 25, 5, 20, 15];
      let result = array.sort();
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

Output

As we can see the output, the sort() method sorted the array elements as strings based on their UTF-16 code units.

10,15,20,25,5

Example 2

Here, we are passing a compare function “(a, b) => {return a - b}” that sorts the array elements in ascending order.

<html>
<body>
   <p id="demo"></p>
   <script>
      const array = [10, 25, 5, 20, 15];
      array.sort((a,b) => {return a-b});
      document.getElementById("demo").innerHTML = array;
   </script>
</body>
</html>

After executing the program, the array elements will be sorted in ascending order.

Output

5,10,15,20,25

Example 3

Here, we are passing a compare function “(a, b) => {return b - a}” that sorts the array elements in descending order.

<html>
<body>
   <p id="demo"></p>
   <script>
      const array = [10, 25, 5, 20, 15];
      array.sort((a,b) => {return b-a});
      document.getElementById("demo").innerHTML = array;
   </script>
</body>
</html>

After executing the program, the array elements will be sorted in descending order.

Output

25,20,15,10,5

Example 4

If the array elements are "string values", the sort() method will sort them in dictionary order, which is ascending order.

<html>
<body>
   <p id="demo"></p>
   <script>
      const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur"];
      let result = animals.sort();
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

After executing the program, we can see that the array elements are sorted in ascending order.

Output

Cheetah,Dinosaur,Elephant,Lion,Tiger
Advertisements