Open In App

JavaScript Array sort() Method

Last Updated : 12 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The JS array.sort() method rearranges the array elements alphabetically and returns a sorted array.

It does not create a new array but updates the original array.

Sorting Array of Strings

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

console.log(ar);

// Sorting the array
ar.sort()

console.log(ar);

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

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 JS

Ascending Order

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

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

Output
[ 10, 100, 20, 25, 40 ]

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.

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

Output
[ 10, 20, 25, 40, 100 ]

Descending Order

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

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

Output
[ 100, 40, 25, 20, 10 ]

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.

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

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

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

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.

// 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);  

Output
[
  { name: 'Jatin', age: 25 },
  { name: 'Rahul', age: 28 },
  { name: 'Vikas', age: 32 },
  { name: 'Rohit', age: 35 }
]
[
  { name: 'Jatin', age: 25 },
  { name: 'Rahul', age: 28 },
  { name: 'Rohi...

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.

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);  

Output
[
  { name: 'Jatin', age: 25 },
  { name: 'Rohit', age: 25 },
  { name: 'Rahul', age: 30 },
  { name: 'Vikas', age: 30 }
]

This article explains the sort stability of the JavaScript sort().

We have a complete list of JavaScript Array methods, to check those please go through this Javascript Array Complete reference article.

JavaScript Array sort() Method- FAQs

What is the use of the array.sort() method in JavaScript?

The sort() method arranges the elements of an array in place and returns the sorted array. By default, it sorts elements as strings and converts elements to strings if they are not already.

What does array sort() return?

The sort() method in JavaScript returns the sorted array after arranging its elements according to the default or provided sorting order. It modifies the original array and does not create a new one.

What is the time complexity of the sort() method in JavaScript?

The time complexity of sort() is generally O(n log n), where n is the number of elements in the array. However, this can vary based on the implementation and browser.

Does sort() modify the original array?

Yes, sort() modifies the original array. It sorts the elements of the array in place and returns the sorted array.

Does sort() handle edge cases like undefined or null values?

Yes, sort() handles undefined and null values by treating them as undefined when comparing (ECMAScript specification).



Next Article

Similar Reads

three90RightbarBannerImg