...

/

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...

Solution explanation

  • Lines 2–23: We define three interchangeable sorting strategy classes: NameSortStrategyDateSortStrategy, and ScoreSortStrategy.

    • Each class exposes a .sort() method, ensuring all strategies follow a common interface.

    • NameSortStrategy alphabetically sorts by the name field.

    • DateSortStrategy compares Date objects to sort chronologically.

    • ScoreSortStrategy orders by score in descending order. ...