JavaScript Tutorials

JavaScript Array sort()
Syntax & Examples

Array.sort() method

The sort() method of the Array class in JavaScript sorts the elements of an array in place and returns the sorted array.


Syntax of Array.sort()

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

1.
sort()

This method sorts the elements of an array in place in ascending, UTF-16 code unit order.

Returns value of type Array.

2.
sort(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 sorts the elements of an array in place according to the compare function.

Returns value of type Array.



✐ Examples

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

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

For example,

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

JavaScript Program

const arr = [5, 3, 8, 1, 2];
const sortedArr = arr.sort();
console.log(sortedArr);

Output

[1, 2, 3, 5, 8]

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

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

For example,

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

JavaScript Program

const strArr = ['banana', 'apple', 'cherry', 'date'];
const sortedStrArr = strArr.sort();
console.log(sortedStrArr);

Output

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

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

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

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 sort() 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 to the console using console.log() method to see the sorted array.

JavaScript Program

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

Output

[8, 5, 3, 2, 1]

Summary

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