Problem: Sort Reports with Pluggable Algorithms
Problem statement
You’re building a report generator for a logistics dashboard. The dataset must be sortable by name, date, or score, depending on user preference.
Sorting logic often ends up buried in long if blocks that grow with each new sorting rule. This makes the code harder to extend and maintain over time. Design the system using the Strategy Pattern so new sorting behaviors can be added safely without touching the main flow.
Goal
Implement both parts of the Strategy Pattern:
Strategies:
NameSortStrategy,DateSortStrategy, andScoreSortStrategyclasses—each must define.sort(data), implementing a distinct sorting rule.Context:
ReportSorterclass that receives a sorting strategy in its constructor, delegates to it in.sort(), and allows swapping strategies via.setStrategy().
Constraints
ReportSortermust not useiforswitchto pick the strategy.Strategies must not depend on each other.
The code should work when new strategies are added without modifying
ReportSorter.
Sample output
The examples below illustrate what the output should look like:
const reports = [{ name: 'Zara', date: '2023-11-01', score: 92 },{ name: 'Alice', date: '2023-10-15', score: 78 },{ name: 'Bob', date: '2023-11-03', score: 85 }];// Instantiate sorter with different strategiesconst sorter = new ReportSorter(new NameSortStrategy());console.log('By name:', sorter.sort(reports));/* Expected output:By name: [ { name: 'Alice', ... }, { name: 'Bob', ... }, { name: 'Zara', ... } ]*/sorter.setStrategy(new DateSortStrategy());console.log('By date:', sorter.sort(reports));/* Expected output:By date: [ { name: 'Alice', ... }, { name: 'Zara', ... }, { name: 'Bob', ... } ]*/sorter.setStrategy(new ScoreSortStrategy());console.log('By score:', sorter.sort(reports));/* Expected output:By score: [ { name: 'Zara', ... }, { name: 'Bob', ... }, { name: 'Alice', ... } ]*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Sort Reports with Pluggable Algorithms
Problem statement
You’re building a report generator for a logistics dashboard. The dataset must be sortable by name, date, or score, depending on user preference.
Sorting logic often ends up buried in long if blocks that grow with each new sorting rule. This makes the code harder to extend and maintain over time. Design the system using the Strategy Pattern so new sorting behaviors can be added safely without touching the main flow.
Goal
Implement both parts of the Strategy Pattern:
Strategies:
NameSortStrategy,DateSortStrategy, andScoreSortStrategyclasses—each must define.sort(data), implementing a distinct sorting rule.Context:
ReportSorterclass that receives a sorting strategy in its constructor, delegates to it in.sort(), and allows swapping strategies via.setStrategy().
Constraints
ReportSortermust not useiforswitchto pick the strategy.Strategies must not depend on each other.
The code should work when new strategies are added without modifying
ReportSorter.
Sample output
The examples below illustrate what the output should look like:
const reports = [{ name: 'Zara', date: '2023-11-01', score: 92 },{ name: 'Alice', date: '2023-10-15', score: 78 },{ name: 'Bob', date: '2023-11-03', score: 85 }];// Instantiate sorter with different strategiesconst sorter = new ReportSorter(new NameSortStrategy());console.log('By name:', sorter.sort(reports));/* Expected output:By name: [ { name: 'Alice', ... }, { name: 'Bob', ... }, { name: 'Zara', ... } ]*/sorter.setStrategy(new DateSortStrategy());console.log('By date:', sorter.sort(reports));/* Expected output:By date: [ { name: 'Alice', ... }, { name: 'Zara', ... }, { name: 'Bob', ... } ]*/sorter.setStrategy(new ScoreSortStrategy());console.log('By score:', sorter.sort(reports));/* Expected output:By score: [ { name: 'Zara', ... }, { name: 'Bob', ... }, { name: 'Alice', ... } ]*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.