 formatResults = (results) ->
   table = new Table(head: ['Source File', 'Memory Usage (blocks)'], colWidths: [50, 25])
   total = 0
-  for result in results
+  sortedResults = results.sort((a, b) -> a.memoryUsage - b.memoryUsage)
+
+  for result in sortedResults
     table.push([result.sourceFile, result.memoryUsage])
     total += result.memoryUsage
 

module.exports = (options) ->
  files = glob.sync('./src/main/**/*.coffee')

  howMuchMemoryForAMD options, (amdError, amdResults) ->
    if amdError
      gutil.log "error!", amdError
    else
      async.mapSeries files, howMuchMemoryForFile(options), (filesError, fileResults) ->
        if filesError
          gutil.log "error!", filesError
        else
          console.log formatResults(fileResults.concat([amdResults]))

async = require 'async'
espruino = require '../../gulp-espruino/src/gulp-espruino'
Table = require 'cli-table'
Q = require 'q'

howMuchMemory = (options, sourceFile, sourceFiles, callback) ->
  gulp.src(sourceFiles)
