JavaScript Array sort() Method

The JavaScript Array.sort() method sorts the elements of an array in place and returns the sorted array. By default, it sorts the elements as strings in ascending order. However, you can provide a custom sorting function to define how the elements should be ordered.

For example, array.sort() is useful when you need to rearrange elements in an array based on a particular sorting logic.

Syntax

The syntax for the Array.sort() method is:

plaintext
array.sort([compareFunction])

Parameters

The Array.sort() method accepts one optional parameter:

  • compareFunction (Optional): A function that defines the sort order. It takes two arguments:
    • firstElement: The first element to compare.
    • secondElement: The second element to compare. If the function returns:
  • A negative value, firstElement comes before secondElement.
  • A positive value, firstElement comes after secondElement.
  • 0, both elements are treated as equal.

Return Value

The method returns the sorted array. It modifies the original array in place.

Examples of JavaScript Array.sort() Method

Example 1: Sorting Strings in Alphabetical Order

By default, the sort() method sorts elements as strings in alphabetical order.

javascript
const fruits = ['banana', 'apple', 'mango'];
fruits.sort();
console.log(fruits);  // Output: ['apple', 'banana', 'mango']

Explanation: The sort() method arranges the strings in lexicographical order (A to Z), resulting in ['apple', 'banana', 'mango'].

Example 2: Sorting Numbers Without a Compare Function

When sorting numbers without a compare function, the sort() method converts numbers to strings and sorts them in lexicographical order.

javascript
const numbers = [10, 1, 21, 2];
numbers.sort();
console.log(numbers);  // Output: [1, 10, 2, 21]

Explanation: The sort() method treats the numbers as strings and sorts them based on the first character, which can lead to unexpected results like [1, 10, 2, 21].

Example 3: Sorting Numbers with a Compare Function

To correctly sort numbers in ascending or descending order, you need to provide a compare function.

javascript
const numbers = [10, 1, 21, 2];
numbers.sort((a, b) => a - b);  // Ascending order
console.log(numbers);  // Output: [1, 2, 10, 21]

Explanation: The compare function (a, b) => a - b ensures the numbers are sorted in numerical order by comparing the differences between elements.

Example 4: Sorting Numbers in Descending Order

You can sort numbers in descending order by reversing the compare function.

javascript
const numbers = [10, 1, 21, 2];
numbers.sort((a, b) => b - a);  // Descending order
console.log(numbers);  // Output: [21, 10, 2, 1]

Explanation: The compare function (a, b) => b - a sorts the numbers in descending order, from largest to smallest.

Example 5: Sorting Strings in Reverse Alphabetical Order

You can provide a custom compare function to sort strings in reverse alphabetical order.

javascript
const fruits = ['banana', 'apple', 'mango'];
fruits.sort((a, b) => b.localeCompare(a));
console.log(fruits);  // Output: ['mango', 'banana', 'apple']

Explanation: The localeCompare() method is used in the compare function to sort the strings in reverse alphabetical order (Z to A).

JavaScript

8124

165

Related Articles