// Drive · Apps Script

Sort Drive files by created or modified date with Apps Script.

DriveApp's file iterator returns files in no guaranteed order. Learn how to collect files into an array and sort them by getDateCreated() or getLastUpdated() before processing.

I need to process my Google Drive files in date order but the DriveApp iterator gives me files in an unpredictable sequence I can't control.

The script

copy · paste · trigger
sortDriveFiles.gs
Apps Script
// Sort files in a folder by created or last-modified date
function getSortedFiles(folderId, sortBy) {
  var folder = DriveApp.getFolderById(folderId);
  var iterator = folder.getFiles();
  var files = [];

  while (iterator.hasNext()) {
    files.push(iterator.next());
  }

  // sortBy: 'created' or 'modified'
  files.sort(function(a, b) {
    var dateA = sortBy === 'created' ? a.getDateCreated() : a.getLastUpdated();
    var dateB = sortBy === 'created' ? b.getDateCreated() : b.getLastUpdated();
    return dateA - dateB;
  });

  return files;
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

Why the iterator won't sort for you

DriveApp.getFolderById(id).getFiles() hands back a FileIterator, not an array. The order it returns files is determined by Drive's internal indexing, and there's no sort parameter you can pass to change it. Calling hasNext() and next() in a loop gives you whatever sequence Drive feels like that day — usually insertion order, sometimes not.

The fix is straightforward: drain the iterator into a plain JavaScript array, then use Array.sort() with a comparator. Date objects in Apps Script subtract cleanly (dateA - dateB returns a number), so the comparator is one line.

Choosing between getDateCreated and getLastUpdated

getDateCreated() returns the timestamp from when the file first appeared in Drive. That date is stable and never changes. getLastUpdated() returns the last time file content or metadata was edited — it moves any time someone renames the file, edits a Sheet cell, or replaces the file's contents.

The first time I used this, I sorted by getLastUpdated() expecting a clean chronological archive and got a mess because auto-saves on open Sheets had bumped half the files to the current minute. If you're building an archive pipeline or a report covering a date range of creation, use getDateCreated(). If you're looking for recently touched files to process or audit, getLastUpdated() is correct.

Both methods return a JavaScript Date object, so you can also call .getTime() on them to get a Unix millisecond integer if you need to store or compare the values outside the sort comparator.

Memory limits and large folders

This pattern loads every file reference into memory at once. File objects themselves are lightweight (they're just API handles, not the file contents), so a folder with a few hundred files is no problem. Apps Script's heap limit only becomes relevant if you're also reading file content in the same loop — for pure metadata sorting, I've run this against folders with 2,000 files without issue.

If your folder is deeper than one level, you'll need to recurse through subfolders with getFolders() and collect files from each. Keep a running array at the outer scope and push into it from each recursive call before sorting at the end.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Does getFiles() ever return files in date order by default?
No. The Drive API does not expose a sort parameter through DriveApp. The FileIterator order is undefined and should not be relied on across runs.
How do I sort newest-first instead of oldest-first?
Reverse the comparator: return dateB - dateA instead of dateA - dateB. That flips the sort direction without any other changes.
Can I filter to only files modified in the last 30 days before sorting?
Yes. After draining the iterator, chain a filter before the sort: files = files.filter(function(f) { return f.getLastUpdated() >= cutoff; }); where cutoff is a Date set to 30 days ago.
Does this work with DriveApp.searchFiles() results too?
Yes. searchFiles() also returns a FileIterator, so the same drain-then-sort pattern applies. Pass a Drive query string like 'mimeType = "application/pdf"' to searchFiles() and then sort the resulting array by date.
// one good script a week

Get a working Apps Script snippet in your inbox, weekly.