Sorting Stability and When to Use Which Sort
📖 Scenario: You work in a small bookstore. You have a list of books with their titles and prices. You want to sort the books by price but keep the original order of books with the same price.This helps you keep the order in which books arrived while sorting by price.
🎯 Goal: You will create a list of books, set a price threshold, sort the books by price using a stable sort, and then print the sorted list showing the order is kept for books with the same price.
📋 What You'll Learn
Create a list called
books with these exact entries: {Title: "Book A", Price: 30}, {Title: "Book B", Price: 20}, {Title: "Book C", Price: 30}, {Title: "Book D", Price: 10}Create an integer variable called
priceThreshold and set it to 25Use Go's
sort.SliceStable to sort books by Price in ascending orderPrint the sorted list in the format:
Title: <Title>, Price: <Price> for each book💡 Why This Matters
🌍 Real World
Sorting products or items by price or rating while keeping the order of items with the same value is common in e-commerce and inventory management.
💼 Career
Understanding stable sorting helps in data processing roles where order preservation is important, such as in databases, UI lists, and report generation.
Progress0 / 4 steps