JavaScript Array sort() method


The sort() method in JavaScript is used to sort the elements of an array in place and return the sorted array. The sorting can be done either in ascending or descending order, depending on the provided compareFunction. If no compareFunction is provided, the array elements are sorted as strings in ascending order.

Syntax:

array.sort(compareFunction);
  • compareFunction (optional): A function that defines the sort order. It takes two arguments (let's call them a and b) and returns:
    • A negative value if a should be sorted before b.
    • A positive value if a should be sorted after b.
    • Zero if a and b are considered equal in the sort order.

Return Value:

  • The sorted array. The original array is modified.

Key Points:

  • In-place sorting: The original array is modified; it does not create a new array.
  • Default behavior: Without a compareFunction, elements are converted to strings and sorted according to their Unicode code point order. This can lead to unexpected results when sorting numbers.
  • Stability: The sort() method is not guaranteed to be stable in JavaScript (meaning the order of equal elements is not guaranteed to remain the same).

Example 1: Basic usage (Sorting strings)

let fruits = ['banana', 'apple', 'orange', 'kiwi']; fruits.sort(); // Sorts the array in ascending order console.log(fruits); // ['apple', 'banana', 'kiwi', 'orange']

Example 2: Sorting numbers with a compare function

let numbers = [40, 100, 1, 5, 25, 10]; numbers.sort((a, b) => a - b); // Sorts the numbers in ascending order console.log(numbers); // [1, 5, 10, 25, 40, 100]

Example 3: Sorting numbers in descending order

let numbers = [40, 100, 1, 5, 25, 10]; numbers.sort((a, b) => b - a); // Sorts the numbers in descending order console.log(numbers); // [100, 40, 25, 10, 5, 1]

Example 4: Sorting objects by a property

let people = [ { name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 } ]; people.sort((a, b) => a.age - b.age); // Sorts people by age in ascending order console.log(people); /* [ { name: 'Jane', age: 25 }, { name: 'John', age: 30 }, { name: 'Jim', age: 35 } ] */

Example 5: Sorting strings with a custom order

let animals = ['elephant', 'dog', 'cat', 'bat']; animals.sort((a, b) => a.length - b.length); // Sorts by the length of the strings console.log(animals); // ['cat', 'dog', 'bat', 'elephant']

Example 6: Default behavior with numbers

let numbers = [10, 1, 21, 2]; numbers.sort(); // Sorts as strings by default console.log(numbers); // [1, 10, 2, 21] (not the expected numerical order)

Summary:

  • The sort() method is a powerful tool for sorting arrays in JavaScript.
  • By providing a compareFunction, you can control the sorting order, whether it's numerical or based on specific properties of objects.
  • Be cautious when using the default behavior for numerical arrays, as it may yield unexpected results by treating numbers as strings. Always use a compareFunction for sorting numbers to ensure correct ordering.