My timers list looks like this:
timer 1 => { startDate = 17/01/2019 11PM, endDate = 18/01/2019 9AM }
timer 2 => { startDate = 18/01/2019 7AM, endDate = 18/01/2019 1PM }
timer 3 => { startDate = 18/01/2019 12PM, endDate = 18/01/2019 10PM }
timer 4 => { startDate = 18/01/2019 11PM, endDate = 19/01/2019 9AM }
I am trying to group these timers based on specific logic.
- The first timer starts on 17/01/2019 at 11PM and ends the next day. It should be grouped under 17/01/2019 (1 timer).
- Timers 2 and 3 fall within 18/01/2019; they should be grouped together under 18/01/2019 (2 timers).
- Timer 4 starts on 18/01/2019 but ends on 19/01/2019; it should be in a separate group under 19/01/2019 (1 timer).
Here is the code I have been using:
let sortedTimers = timers.sort((a, b) => {
return a.startDate.isBefore(b.startDate) ? -1 : 1;
});
let groupedByDate: { [date: string]: Timer[] } = _.groupBy(sortedTimers, timer => {
let start = this.dateFormatter.toDayOfWeekLongDate(Timer.startDate);
let end = this.dateFormatter.toDayOfWeekLongDate(Timer.endDate);
return start == end? start:end; // wrong logic
});
The current code only returns two groups: 18/01/2019 (3 timers) and 19/01/201 (1 timer). However, I need three groups. I acknowledge that there is an issue with the logic in the code.
I am utilizing lodash for grouping purposes. Any guidance on how to correct the logic would be greatly appreciated. Thank you.