Skip to content

Commit 8ad29dc

Browse files
author
Colby Gallup
authored
Merge pull request #1158 from codeRIT/bh-1157
Added event sorting
2 parents 19df6fc + a787758 commit 8ad29dc

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff 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

152172
function 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

0 commit comments

Comments
 (0)