JavaScript sort() Method

Introduction

The sort() method in JavaScript is used to arrange array elements in a specific order. By default, it sorts elements as strings in ascending order, but it can also sort numbers, objects, dates, and custom values using comparison functions.

What is JavaScript sort() Method?

The JavaScript sort() method arranges array elements in ascending or custom order.

Syntax of sort()

Basic syntax:


array.sort()

Custom sorting:


array.sort(
function(a,b){
}
)

Where:

  • a is the first element.
  • b is the second element.

Basic Examples of sort()

We have many basic examples of the sort() method

Example 1: Sort Strings


let fruits= ["Banana", "Apple", "Mango"];

fruits.sort();

console.log(fruits);

Output:

[“Apple”, “Banana”, “Mango”]

Example 2: sort() Treats Values as Strings


let nums = [100,20,300];
nums.sort();
console.log(nums);

Output:

[ 100, 20, 300 ]

Note: This is an unexpected result because sorting occurs alphabetically.

Example 3: Correct Number Sorting with Ascending Order

In this example, we will use the comparison function.


let nums = [100,20,300];
nums.sort((a,b)=> a-b);
console.log(nums);

Output:

[ 20, 100, 300 ]

Example 4: Correct Number Sorting with Descending Order


let nums = [100,20,300];
nums.sort((a,b)=> b-a);
console.log(nums);

Output:

[ 300, 100, 20 ]

Sorting Objects

In the example below, we will sort the Object value.


let users=[
{name:"John",age:35},
{name:"Tom",age:30},
{name:"Rom",age:25}
];

users.sort(
(a,b)=>
a.age-b.age
);

console.log(users);

Output:

[ { name: ‘Rom’, age: 25 }, { name: ‘Tom’, age: 30 }, { name: ‘John’, age: 35 } ]

Sorting Strings in Descending Order

Syntax:


arrayName.sort().reverse();

Example:


let fruits= ["Apple", "Banana", "Mango"];
fruits.sort().reverse();
console.log(fruits);

Output:

[“Mango”,”Banana”,”Apple”]

Real-Life Example

You will see some real-life examples of sorting.

Real-Life Example 1: E-commerce Product Prices

In the example below, we will sort products by price.


let prices = [500,200,800];
prices.sort( (a,b)=> a-b);
console.log(prices);

Output:

[200,500,800]

Real-Life Example 2: Student Marks Ranking

In the example below, you will see marks sorted from highest to lowest.


let marks= [90,70,80];
marks.sort((a,b)=> b-a);
console.log(marks);

Output:

[90,80,70]

Difference Between sort() and reverse()

Feature sort() reverse()
Purpose Arrange values Reverse order
Sorting Yes No
Example
[3,1,2]
.sort()

// Output:
[1,2,3]
[1,2,3]
.reverse()

// Output:
[3,2,1]

Common Mistakes in sort()

I will show some common mistakes when using the sort() method.

Mistake 1: Sorting Numbers Without a Comparison Function

Wrong:


let nums = [100,20,3];
nums.sort();
console.log(nums);

Output:

[ 100, 20, 3 ]

Correct:


let nums = [100,20,3];
nums.sort((a,b) => a-b);
console.log(nums);

Output:

[ 3, 20, 100 ]

Mistake 2: Forgetting Return in Function

Wrong:


let nums = [100,3,20];
nums.sort((a,b) => {
a-b
}
);
console.log(nums);

Output:

[ 100, 3, 20 ]

Correct:


let nums = [100,3,20];
nums.sort((a,b) => {
return a-b
}
);
console.log(nums);

Output:

[ 3, 20, 100 ]

JavaScript Array Sorting – Interview Questions

Q 1: Which method sorts arrays?
Ans: sort().
Q 2: Default sort order?
Ans: Alphabetical.
Q 3: How to sort numbers correctly?
Ans: Use the compare function.
Q 4: Does sort modify the original array?
Ans: Yes.
Q 5: Can sort be descending?
Ans: Yes.

JavaScript Array Sorting – Objective Questions (MCQs)

Q1. Which method is used to sort an array in JavaScript?






Q2. By default, sort() sorts elements as ______.






Q3. Which function is used to sort numbers correctly?






Q4. What does arr.sort((a, b) => a - b) do?






Q5. Does sort() modify the original array?






Conclusion

The JavaScript sort() method is a powerful array method used to organize data efficiently. It is commonly used on e-commerce websites, in dashboards, APIs, and applications that require ordered data.

One important thing to remember is that sort() modifies the original array and sorts numbers incorrectly when no comparison function is provided.

Related JavaScript Tutorials