Skip to content

Commit aad73a5

Browse files
committed
fix: events not sorted correctly
Events were not sorted correctly since the allDayBottom option was added. fixes #1240, fixes #1244
1 parent 8770e3e commit aad73a5

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/functions/sort_events.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dayjs from 'dayjs';
2+
import EventClass from '../lib/event.class';
23

34
// Function to sort events
45
export default function sortEvents(events, config) {
@@ -40,21 +41,29 @@ export default function sortEvents(events, config) {
4041

4142
// Combine sorted all-day and non-all-day events
4243
const sortedEvents = [...events];
43-
sortedEvents.forEach((event, index) => {
44-
const allDayIndex = allDayEvents.findIndex((adEvent) => adEvent.id === event.id);
45-
if (allDayIndex !== -1) {
44+
45+
// Create an array to store the all day events.
46+
const allDayEventsArray: EventClass[] = [];
47+
48+
// Iterate over the sorted events array and move all of the all day events to the allDayEvents array.
49+
sortedEvents.forEach((event: EventClass, index) => {
50+
if (event.isAllDayEvent) {
51+
allDayEventsArray.push(event);
4652
sortedEvents.splice(index, 1);
47-
sortedEvents.push(allDayEvents[allDayIndex]);
4853
}
4954
});
5055

51-
// Apply all-day sorting based on the 'config.allDayBottom' boolean
56+
// If config.allDayBottom is true, add the all day events to the end of the sorted events array.
5257
if (config.allDayBottom) {
53-
sortedEvents.sort((a, b) => b.startDateTime.diff(a.startDateTime));
54-
} else {
55-
sortedEvents.sort((a, b) => a.startDateTime.diff(b.startDateTime));
58+
sortedEvents.push(...allDayEventsArray);
59+
}
60+
61+
// Otherwise, add the all day events to the beginning of the sorted events array.
62+
else {
63+
sortedEvents.unshift(...allDayEventsArray);
5664
}
5765

66+
5867
if (config.sortBy === 'milestone') {
5968
// Move finished events to the bottom when sorting by milestone
6069
sortedEvents.sort((a, b) => {
@@ -64,6 +73,6 @@ export default function sortEvents(events, config) {
6473
return 0;
6574
});
6675
}
67-
76+
console.log(sortedEvents);
6877
return sortedEvents;
6978
}

0 commit comments

Comments
 (0)