File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -147,7 +147,27 @@ $('.day-first').click(function() {
147147} ) ;
148148$ ( '.day-second' ) . click ( showSecondDayEvents ) ;
149149
150- // Dynamic schedule code (sample API data)
150+ // Dynamic schedule code
151+
152+ function compareEvents ( a , b ) {
153+ // We can sort by start/end here because the ISO 8061
154+ // timestamps given by the server are lexicographically
155+ // sortable.
156+
157+ if ( a . start < b . start ) { // if a starts before b...
158+ return - 1 ; // ...then a goes before b
159+ } else if ( a . start > b . start ) { // if a starts after b...
160+ return 1 ; // ...then b goes before a
161+ } else {
162+ if ( a . end < b . end ) { // if a ends before b...
163+ return - 1 ; // ...then a goes before b
164+ } else if ( a . end > b . end ) { // if a ends after b...
165+ return 1 ; // ...then b goes before a
166+ } else {
167+ return 0 ;
168+ }
169+ }
170+ }
151171
152172function convertDate ( date ) {
153173 let output = '' ;
@@ -185,6 +205,9 @@ function handleEventData(events) {
185205 showSecondDayEvents ( ) ;
186206 }
187207
208+ // need to sort events by start/end times instead of IDs
209+ events . sort ( compareEvents ) ;
210+
188211 events . forEach ( event => {
189212 let startDate = new Date ( event . start ) ; // convert ISO 8601 -> Date object
190213
You can’t perform that action at this time.
0 commit comments