...
/Solution: Sort Reports with Pluggable Algorithms
Solution: Sort Reports with Pluggable Algorithms
Implement and connect both the strategies and the context to enable runtime switching between sorting algorithms.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 2–23: We define three interchangeable sorting strategy classes:
NameSortStrategy,DateSortStrategy, andScoreSortStrategy.Each class exposes a
.sort()method, ensuring all strategies follow a common interface.NameSortStrategyalphabetically sorts by thenamefield.DateSortStrategycomparesDateobjects to sort chronologically.ScoreSortStrategyorders byscorein descending order. ...