Introduction
Sorting means arranging data in a specific order, usually ascending or descending. Sorting is an important Data Structure concept because organized data makes searching, processing, and analysis easier. In this chapter, we will practice sorting through practical JavaScript questions. The exercises focus on different sorting situations, including numbers, strings, objects, ascending and descending order, and creating reusable sorting logic. Data Structure Sorting practice questions with solutions help to build concepts.
Question 1: Sort Numbers in Ascending Order
Questions
Write a JavaScript program to sort the following numbers from smallest to largest.
let numbers = [40, 10, 30, 50, 20];
Solution
let numbers = [40, 10, 30, 50, 20];
numbers.sort((a, b) => a - b);
console.log(numbers);
Output
[10, 20, 30, 40, 50]
The comparison function a - b sorts numbers in ascending order.
Question 2: Sort Numbers in Descending Order
Questions
Sort the following numbers from largest to smallest.
let numbers = [15, 80, 35, 60, 25];
Solution
let numbers = [15, 80, 35, 60, 25];
numbers.sort((a, b) => b - a);
console.log(numbers);
Output
[80, 60, 35, 25, 15]
The comparison function b - a produces descending order.
Question 3: Sort an Array Without Using sort()
Questions
Sort the array in ascending order without using JavaScript’s built-in sort() method.
let numbers = [5, 2, 8, 1, 3];
Solution
let numbers = [5, 2, 8, 1, 3];
for (let i = 0; i < numbers.length - 1; i++) {
for (let j = 0; j < numbers.length - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
let temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
console.log(numbers);
Output
[1, 2, 3, 5, 8]
This approach repeatedly compares neighboring elements and swaps them when they are in the wrong order.
Question 4: Sort Strings Alphabetically
Questions
Sort the following programming languages alphabetically.
let languages = ["Python", "JavaScript", "C++", "Java", "Ruby"];
Solution
let languages = ["Python", "JavaScript", "C++", "Java", "Ruby"];
languages.sort();
console.log(languages);
Output
["C++", "Java", "JavaScript", "Python", "Ruby"]
JavaScript’s default string sorting compares strings lexicographically.
Question 5: Sort Strings by Length
Questions
Sort the following words from shortest to longest.
let words = ["HTML", "JavaScript", "CSS", "Python"];
Solution
let words = ["HTML", "JavaScript", "CSS", "Python"];
words.sort((a, b) => a.length - b.length);
console.log(words);
Output
["CSS", "HTML", "Python", "JavaScript"]
The comparison uses the length of each string instead of alphabetical order.
Question 6: Sort Students by Marks
Questions
Sort the students according to their marks from highest to lowest.
let students = [
{ name: "Rahul", marks: 75 },
{ name: "Priya", marks: 92 },
{ name: "Aman", marks: 68 },
{ name: "Neha", marks: 85 }
];
Solution
let students = [
{ name: "Rahul", marks: 75 },
{ name: "Priya", marks: 92 },
{ name: "Aman", marks: 68 },
{ name: "Neha", marks: 85 }
];
students.sort((a, b) => b.marks - a.marks);
console.log(students);
Output
[
{ name: "Priya", marks: 92 },
{ name: "Neha", marks: 85 },
{ name: "Rahul", marks: 75 },
{ name: "Aman", marks: 68 }
]
Here, the marks property determines the sorting order.
Question 7: Sort Numbers Using Selection Sort
Questions
Implement Selection Sort to arrange the numbers in ascending order.
let numbers = [64, 25, 12, 22, 11];
Solution
let numbers = [64, 25, 12, 22, 11];
for (let i = 0; i < numbers.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
let temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
}
console.log(numbers);
Output
[11, 12, 22, 25, 64]
Selection Sort repeatedly finds the smallest remaining element and places it in the correct position.
Question 8: Sort Numbers Using Insertion Sort
Questions
Implement Insertion Sort to arrange the numbers in ascending order.
let numbers = [9, 5, 1, 4, 3];
Solution
let numbers = [9, 5, 1, 4, 3];
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
}
console.log(numbers);
Output
[1, 3, 4, 5, 9]
Insertion Sort takes one element at a time and inserts it into its correct position among the already sorted elements.
Question 9: Sort an Array Using Merge Sort
Questions
Implement Merge Sort to sort the following array.
let numbers = [38, 27, 43, 3, 9, 82, 10];
Solution
function mergeSort(array) {
if (array.length <= 1) {
return array;
}
let mid = Math.floor(array.length / 2);
let left = mergeSort(array.slice(0, mid));
let right = mergeSort(array.slice(mid));
return merge(left, right);
}
function merge(left, right) {
let result = [];
let i = 0;
let j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}
while (i < left.length) {
result.push(left[i]);
i++;
}
while (j < right.length) {
result.push(right[j]);
j++;
}
return result;
}
let numbers = [38, 27, 43, 3, 9, 82, 10];
console.log(mergeSort(numbers));
Output
[3, 9, 10, 27, 38, 43, 82]
Merge Sort divides the array into smaller parts, sorts those parts, and then merges them together.
Question 10: Sort Products by Price
Questions
Sort the following products from the lowest price to the highest price.
let products = [
{ name: "Keyboard", price: 1200 },
{ name: "Mouse", price: 700 },
{ name: "Monitor", price: 8500 },
{ name: "Headphones", price: 2000 }
];
Solution
let products = [
{ name: "Keyboard", price: 1200 },
{ name: "Mouse", price: 700 },
{ name: "Monitor", price: 8500 },
{ name: "Headphones", price: 2000 }
];
products.sort((a, b) => a.price - b.price);
console.log(products);
Output
[
{ name: "Mouse", price: 700 },
{ name: "Keyboard", price: 1200 },
{ name: "Headphones", price: 2000 },
{ name: "Monitor", price: 8500 }
]
The comparison function uses the price property to determine the order.
Key Takeaways
- Sorting arranges data into a specific order.
- Numbers can be sorted in ascending or descending order.
- Strings can be sorted alphabetically.
- Custom comparison functions can control how JavaScript sorts data.
- Arrays of objects can be sorted using object properties.
- Bubble Sort repeatedly compares neighboring elements.
- Selection Sort selects the smallest remaining element.
- Insertion Sort inserts elements into their correct position.
- Merge Sort uses a divide-and-conquer approach.
- JavaScript provides the built-in
sort()method for sorting arrays. - Numeric arrays should use a comparison function with
sort(). - Sorting is often useful before applying efficient searching techniques such as Binary Search.
FAQs
1. What is Sorting in Data Structures?
Sorting is the process of arranging data in a particular order, such as ascending or descending order.
2. Why is sorting important?
Sorting makes data easier to read, process, analyze, and search. Some algorithms, such as Binary Search, also require sorted data.
3. What is the difference between ascending and descending order?
Ascending order arranges values from smallest to largest, while descending order arranges them from largest to smallest.
4. What is Bubble Sort?
Bubble Sort repeatedly compares adjacent elements and swaps them when they are in the wrong order.
5. What is Selection Sort?
Selection Sort repeatedly finds the smallest element from the unsorted portion and places it at the correct position.
6. What is Insertion Sort?
Insertion Sort builds a sorted portion of the array by taking one element at a time and inserting it into its appropriate position.
7. What is Merge Sort?
Merge Sort is a divide-and-conquer sorting algorithm that divides an array into smaller parts, sorts them, and merges the sorted parts.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
