JavaScript Array sort() Method

Last Updated : 16 Jan, 2026

The sort() method in JavaScript is used to arrange array elements in a specific order, usually alphabetically or in ascending order. It modifies the original array and returns the sorted result.

  • Sorts elements alphabetically by default (as strings)
  • Modifies the original array instead of creating a new one
  • Can use a compare function for custom sorting (e.g., numbers)

Sorting Array of Strings

JavaScript
// Original array
let ar = ["JS", "HTML", "CSS"];

console.log(ar);

// Sorting the array
ar.sort()

console.log(ar);

Syntax

arr.sort(compareFunction);

Parameters

  • arr: The array to be sorted.
  • compareFunction (Optional): A function that defines the sort order. If omitted, the array elements are sorted based on their string Unicode code points.

Return value: This method returns the reference of the sorted original array.

Sorting a Numeric Array in Javascript

Ascending Order

Array.sort() sorts the elements in lexicographical order whether a string or number.

JavaScript
const ar = [ 10, 20, 25, 100 , 40]
console.log(ar.sort())

The sorting with JavaScript's sort() is different from languages like C++ , Java and Python. It compares array elements alphabetically as strings rather than numbers. This causes elements to be sorted based on character Unicode values instead of numerical order.

To sort the numeric array property we have to pass a comparator function.

JavaScript
const ar = [ 10, 20, 25, 100 , 40]
console.log(ar.sort((a,b) => a - b))

Descending Order

change the comparator function to sort the array in descending order.

JavaScript
const ar = [ 10, 20, 25, 100 , 40]
console.log(ar.sort((a,b) => b - a))

Please go through this How to Sort Numeric Array using JavaScript?, to know how the JavaScript array sort function works.

Sort Array of Strings in Reverse Order

We can use the string.localeCompare() method in the comparator function to sort in reverse order.

JavaScript
let a = ["JS", "HTML", "CSS"];
console.log(a);
a.sort((x, y) => x.localeCompare(y))
console.log(a);

We can also use the array.reverse() method to sort the array in descending order.

JavaScript
let a = ["JS", "CSS", "HTML"];
a.sort();
a.reverse();
console.log(a);

Output
[ 'JS', 'HTML', 'CSS' ]

Sorting Array of Objects

The array of objects can be sorted on the basis of property values.

JavaScript
// Array of a objects with different names and ages
let a = [
    { name: 'Rahul', age: 28 },
    { name: 'Jatin', age: 25 },
    { name: 'Vikas', age: 32 },
    { name: 'Rohit', age: 35 }
];

// Sort the objects for age
a.sort((x, y) => x.age - y.age);

console.log(a);

// Sort object for names 
a.sort((x, y) => x.name.localeCompare(y.name));

console.log(a);  

Sort Stability

Sort stability means that when sorting, if two items have the same value, their order stays the same as in the original list. A stable sort keeps the order of equal items unchanged during sorting.

Note: Before ECMAScript 2019 (version 10), JavaScript's sort() method did not guarantee sort stability, meaning the original order of equal elements might not be preserved after sorting.

JavaScript
let a = [
    { name: 'Rahul', age: 30 },
    { name: 'Jatin', age: 25 },
    { name: 'Vikas', age: 30 },
    { name: 'Rohit', age: 25 }
];

a.sort((x, y) => x.age - y.age);

console.log(a);  
Comment

Explore