Sorting a List of Fruits with Custom Order in Swift
📖 Scenario: You have a list of fruits, but you want to sort them in a special order that you decide, not just alphabetically.
🎯 Goal: Build a Swift program that sorts a list of fruits using a custom order you define.
📋 What You'll Learn
Create an array called
fruits with the exact values: "apple", "banana", "cherry", "date", "elderberry"Create a dictionary called
priority that assigns each fruit a number representing its custom orderUse the
sorted(by:) method with a custom comparator closure to sort fruits by the priority valuesPrint the sorted array exactly as shown
💡 Why This Matters
🌍 Real World
Sorting items by custom rules is common in apps like shopping lists, games, or task managers where order matters beyond simple alphabetical sorting.
💼 Career
Understanding how to use custom comparators and sorting is important for software developers working on user interfaces, data processing, and any feature that requires ordered data.
Progress0 / 4 steps