11import dayjs from 'dayjs' ;
2+ import EventClass from '../lib/event.class' ;
23
34// Function to sort events
45export 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