code: function sortDependencies( dependencies ) {
  
      // Build the list of keys.
      var keys = [];
      for ( var i = 0; i < dependencies.length; i++ ) {
          keys.push( dependencies[i][0] );
      }
  
      var unsorted = dependencies.slice();    // Entries not yet sorted.
      var sorted_keys = [];                   // Keys already sorted.
      var sorted = [];                        // Entries already sorted.
  
      // Each pass through the map should sort at least one item.
      // This gives us a maximum number of passes as n*(n+1) / 2.
      var max_pass = ( dependencies.length * ( dependencies.length + 1 ) ) / 2;
      for ( var i = 0; i < max_pass; i++ ) {
          // On each pass, we pull out anything that has no dependencies,
          // is depend on an item which has already been sorted, or is
          // dependent on something not in this map.
          var dependency = unsorted.shift();
          var key = dependency[0];
          var dependsOn = dependency[1];
          if ( dependsOn === null
              || sorted_keys.indexOf( dependsOn ) >= 0
              || keys.indexOf( dependsOn ) < 0 ) {
              
              sorted_keys.push( key );
              sorted.push( dependency );
              if ( unsorted.length === 0 ) {
                  // Done
                  break;
              }
          } else {
              unsorted.push( dependency ); // Defer sorting for a later pass.
          }
      }
  
      if ( unsorted.length > 0 ) {
          throw "Dependency map contains a circular reference.";
      }
  
      return sorted;
  }
docs: /*
  Sort an array representing a set of dependencies, such that each item
  in the result comes after any items its dependent upon.
  
  The dependency map is represented as an array with tuple values.
  Each tuple takes the form (key, dependsOn, [data...]), where dependsOn can
  refer to the key of an item that entry depends on. E.g., if the
  map looks like:
  
  [
  ("Bar", "Foo", "Goodbye"),  # Bar depends on Foo
  ("Foo", None, "Hello")      # Foo has no dependencies
  ]
  
  Sorting these dependencies will return the array with the "Foo"
  tuple first, followed by the "Bar" tuple, because Bar depends on Foo.