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:
Example 2: sort() Treats Values as Strings
let nums = [100,20,300];
nums.sort();
console.log(nums);
Output:
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:
Example 4: Correct Number Sorting with Descending Order
let nums = [100,20,300];
nums.sort((a,b)=> b-a);
console.log(nums);
Output:
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:
Sorting Strings in Descending Order
Syntax:
arrayName.sort().reverse();
Example:
let fruits= ["Apple", "Banana", "Mango"];
fruits.sort().reverse();
console.log(fruits);
Output:
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:
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:
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:
Correct:
let nums = [100,20,3];
nums.sort((a,b) => a-b);
console.log(nums);
Output:
Mistake 2: Forgetting Return in Function
Wrong:
let nums = [100,3,20];
nums.sort((a,b) => {
a-b
}
);
console.log(nums);
Output:
Correct:
let nums = [100,3,20];
nums.sort((a,b) => {
return a-b
}
);
console.log(nums);
Output:
JavaScript Array Sorting – Interview Questions
Q 1: Which method sorts arrays?
Q 2: Default sort order?
Q 3: How to sort numbers correctly?
Q 4: Does sort modify the original array?
Q 5: Can sort be descending?
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.