Skip to content

Instantly share code, notes, and snippets.

@leoherzog
Last active January 13, 2017 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoherzog/0d2b2222c068e4cbbbfc3a84edf8f696 to your computer and use it in GitHub Desktop.
Save leoherzog/0d2b2222c068e4cbbbfc3a84edf8f696 to your computer and use it in GitHub Desktop.
Sort your array of CalendarEvent objects that were returned from the Google Apps Script CalendarApp
// copy this to the bottom of your script, then call it on your array of CalendarEvent objects that you got from the CalendarApp
//
// ex:
// var sortedEvents = sortArrayOfCalendarEventsChronologically(events);
// or
// events = sortArrayOfCalendarEventsChronologically(events);
function sortArrayOfCalendarEventsChronologically(array) {
if (!array || array.length == 0) {
return 0;
}
var temp = [];
for (var i in array) {
var startTime = new Date(array[i].getStartTime());
var startTimeMilli = startTime.getTime();
for (var j in temp) {
var iteratorStartTime = temp[j].getStartTime();
var iteratorStartTimeMilli = iteratorStartTime.getTime();
if (startTimeMilli < iteratorStartTimeMilli) {
break;
}
}
temp.splice(j, 0, array[i]);
}
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment