Skip to content

Commit 3bf8a5e

Browse files
author
Colby Gallup
committed
Added event sorting code
1 parent a23ca33 commit 3bf8a5e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ $('.day-second').click(showSecondDayEvents);
149149

150150
// Dynamic schedule code
151151

152+
function sortEvents(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+
}
171+
152172
function convertDate(date) {
153173
let output = '';
154174

@@ -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(sortEvents);
210+
188211
events.forEach(event => {
189212
let startDate = new Date(event.start); // convert ISO 8601 -> Date object
190213

0 commit comments

Comments
 (0)