📖 Scenario: You work at a bookstore that keeps a list of books with their titles and prices. Sometimes, books have the same price. You want to sort the books by price but keep the order of books with the same price as they were originally. This is called a stable sort.Later, you want to understand when to use different sorting methods based on speed and stability.
🎯 Goal: You will create a list of books, add a sorting preference, sort the books by price using a stable sort, and then print the sorted list showing how stable sorting keeps the original order for books with the same price.
📋 What You'll Learn
Create an array of book objects with exact titles and prices
Add a variable to choose sorting method
Use a stable sorting method to sort books by price
Print the sorted list showing title and price
💡 Why This Matters
🌍 Real World
Sorting lists of products, books, or any items by price or other attributes is common in online stores and inventory systems. Stable sorting helps keep the original order when items have the same key, which can be important for user experience.
💼 Career
Understanding sorting stability and when to use stable or unstable sorts is important for software developers working on data processing, UI lists, and performance optimization.
Progress0 / 4 steps
1
Create the list of books
Create an array called books with these exact objects in this order: { title: 'Book A', price: 30 }, { title: 'Book B', price: 20 }, { title: 'Book C', price: 30 }, { title: 'Book D', price: 10 }
DSA Javascript
Hint
Use const books = [ ... ] with objects inside curly braces.
2
Add sorting method choice
Create a variable called useStableSort and set it to true to choose stable sorting
DSA Javascript
Hint
Use const useStableSort = true; to set the sorting method.
3
Sort the books by price using stable sort
Use books.sort() with a compare function that compares a.price and b.price to sort the books by price in ascending order. Use the variable useStableSort to decide if sorting should happen.
DSA Javascript
Hint
Use books.sort((a, b) => a.price - b.price) to sort by price ascending.
4
Print the sorted list of books
Use a for loop with variables book to go through books and print each book's title and price in the format Title: Book A, Price: 30
DSA Javascript
Hint
Use for (const book of books) and console.log with template strings.
Practice
(1/5)
1. What does it mean when a sorting algorithm is stable?
easy
A. It keeps the original order of equal elements after sorting
B. It sorts elements faster than unstable algorithms
C. It uses less memory than unstable algorithms
D. It always sorts in ascending order
Solution
Step 1: Understand stability in sorting
A stable sort keeps the relative order of items with equal keys unchanged.
Step 2: Compare with other options
Speed, memory use, or sorting order do not define stability.
Final Answer:
It keeps the original order of equal elements after sorting -> Option A
Quick Check:
Stable sort = keeps equal elements order [OK]
Hint: Stable means equal items keep their original order [OK]
Common Mistakes:
Confusing stability with speed
Thinking stability means sorting ascending only
Assuming stable sorts use less memory
2. Which of the following is the correct way to call JavaScript's built-in sort method to sort numbers in ascending order?
easy
A. arr.sort()
B. arr.sort((a, b) => a + b)
C. arr.sort((a, b) => b - a)
D. arr.sort((a, b) => a - b)
Solution
Step 1: Understand JavaScript's default sort
By default, arr.sort() sorts elements as strings, not numbers.
Step 2: Use compare function for numeric ascending
The compare function (a, b) => a - b sorts numbers ascending correctly.
Final Answer:
arr.sort((a, b) => a - b) -> Option D
Quick Check:
Numeric ascending sort uses (a, b) => a - b [OK]
Hint: Use (a, b) => a - b for ascending numeric sort [OK]
What is the main reason the original order of equal length strings might not be preserved?
medium
A. Strings cannot be sorted by length
B. JavaScript's sort is unstable in some engines
C. The comparator function is incorrect
D. The array contains duplicates
Solution
Step 1: Check comparator correctness
The comparator (a, b) => a.length - b.length correctly compares string lengths.
Step 2: Understand JavaScript sort stability
JavaScript's sort is stable in modern engines but may be unstable in older engines, causing order change.
Final Answer:
JavaScript's sort is unstable in some engines -> Option B
Quick Check:
Unstable sort breaks equal element order [OK]
Hint: Old JS engines have unstable sort, losing order [OK]
Common Mistakes:
Blaming comparator function
Thinking strings can't be sorted by length
Assuming duplicates cause instability
5. You have a list of student records with name and score. You want to sort by score ascending but keep the original order for students with the same score. Which sorting algorithm should you choose and why?
hard
A. MergeSort, because it is stable and sorts efficiently
B. QuickSort, because it is fast but unstable
C. HeapSort, because it uses less memory
D. BubbleSort, because it is simple and stable
Solution
Step 1: Identify need for stability
We want to keep original order for equal scores, so stability is required.
Step 2: Choose algorithm with stability and efficiency
MergeSort is stable and efficient (O(n log n)), suitable for this task.
Step 3: Compare other options
QuickSort is fast but unstable; HeapSort is unstable; BubbleSort is stable but inefficient.
Final Answer:
MergeSort, because it is stable and sorts efficiently -> Option A
Quick Check:
Stable + efficient = MergeSort [OK]
Hint: Stable and efficient sort for equal keys: MergeSort [OK]
Common Mistakes:
Choosing QuickSort ignoring instability
Picking BubbleSort despite inefficiency
Confusing HeapSort as stable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.