diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php b/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php
index 377f7c4..6b3793e 100644
--- a/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php
@@ -173,7 +173,9 @@ public function getDependentEntities($type, $name) {
     // dependent is at the top. For example, this ensures that fields are
     // always after field storages. This is because field storages need to be
     // created before a field.
-    return array_reverse(array_intersect_key($this->graph, $dependencies));
+    $graph = $this->getGraph();
+    uasort($graph, array($this, 'sortGraph'));
+    return array_replace(array_intersect_key($graph, $dependencies), $dependencies);
   }
 
   /**
@@ -188,7 +190,8 @@ public function sortAll() {
     // Sort by reverse weight and alphabetically. The most dependent entities
     // are last and entities with the same weight are alphabetically ordered.
     uasort($graph, array($this, 'sortGraph'));
-    return array_keys($graph);
+    // Use array_intersect_key() to exclude modules and themes from the list.
+    return array_reverse(array_keys(array_intersect_key($graph, $this->data)));
   }
 
   /**
@@ -228,9 +231,11 @@ protected function createGraphConfigEntityDependencies($entities_to_check) {
     $graph = $this->getGraph();
 
     foreach ($entities_to_check as $entity) {
-      if (isset($graph[$entity]) && !empty($graph[$entity]['reverse_paths'])) {
-        foreach ($graph[$entity]['reverse_paths'] as $dependency => $value) {
-          $dependent_entities[$dependency] = $this->data[$dependency];
+      if (isset($graph[$entity]) && !empty($graph[$entity]['paths'])) {
+        foreach ($graph[$entity]['paths'] as $dependency => $value) {
+          if (isset($this->data[$dependency])) {
+            $dependent_entities[$dependency] = $this->data[$dependency];
+          }
         }
       }
     }
@@ -248,12 +253,13 @@ protected function getGraph() {
       $graph = array();
       foreach ($this->data as $entity) {
         $graph_key = $entity->getConfigDependencyName();
-        $graph[$graph_key]['edges'] = array();
-        $dependencies = $entity->getDependencies('config');
-        if (!empty($dependencies)) {
-          foreach ($dependencies as $dependency) {
-            $graph[$graph_key]['edges'][$dependency] = TRUE;
-          }
+        if (!isset($graph[$graph_key])) {
+          $graph[$graph_key]['edges'] = [];
+        }
+        // Include all dependencies in the graph so that topographical sorting
+        // works.
+        foreach (array_merge($entity->getDependencies('config'), $entity->getDependencies('module'), $entity->getDependencies('theme')) as $dependency) {
+          $graph[$dependency]['edges'][$graph_key] = TRUE;
         }
       }
       $graph_object = new Graph($graph);
diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php
index 2fd6301..f865fa1 100644
--- a/core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php
@@ -222,13 +222,10 @@ public function testConfigEntityUninstall() {
     $this->assertFalse($storage->load('entity1'), 'Entity 1 deleted');
     $this->assertFalse($storage->load('entity2'), 'Entity 2 deleted');
 
-    // Set a more complicated test where dependencies will be fixed.
-    \Drupal::state()->set('config_test.fix_dependencies', array($entity1->getConfigDependencyName()));
-    \Drupal::state()->set('config_test.on_dependency_removal_called', []);
-    // Entity1 will be deleted because it depends on node.
-    $entity1 = $storage->create(
+    // Entity D will be deleted because it depends on node.
+    $entity_d = $storage->create(
       array(
-        'id' => 'entity1',
+        'id' => 'entity_d',
         'dependencies' => array(
           'enforced' => array(
             'module' => array('node', 'config_test')
@@ -236,77 +233,82 @@ public function testConfigEntityUninstall() {
         ),
       )
     );
-    $entity1->save();
+    $entity_d->save();
 
-    // Entity2 has a dependency on Entity1 but it can be fixed because
+    // Entity C has a dependency on Entity1 but it can be fixed because
     // \Drupal\config_test\Entity::onDependencyRemoval() will remove the
     // dependency before config entities are deleted.
-    $entity2 = $storage->create(
+    $entity_c = $storage->create(
       array(
-        'id' => 'entity2',
+        'id' => 'entity_c',
         'dependencies' => array(
           'enforced' => array(
-            'config' => array($entity1->getConfigDependencyName()),
+            'config' => array($entity_d->getConfigDependencyName()),
           ),
         ),
       )
     );
-    $entity2->save();
+    $entity_c->save();
 
-    // Entity3 will be unchanged because it is dependent on Entity2 which can
+    // Entity B will be unchanged because it is dependent on Entity C which can
     // be fixed. The ConfigEntityInterface::onDependencyRemoval() method will
     // not be called for this entity.
-    $entity3 = $storage->create(
+    $entity_b = $storage->create(
       array(
-        'id' => 'entity3',
+        'id' => 'entity_b',
         'dependencies' => array(
           'enforced' => array(
-            'config' => array($entity2->getConfigDependencyName()),
+            'config' => array($entity_c->getConfigDependencyName()),
           ),
         ),
       )
     );
-    $entity3->save();
+    $entity_b->save();
 
-    // Entity4's config dependency will be fixed but it will still be deleted
+    // Entity A's config dependency will be fixed but it will still be deleted
     // because it also depends on the node module.
-    $entity4 = $storage->create(
+    $entity_a = $storage->create(
       array(
-        'id' => 'entity4',
+        'id' => 'entity_a',
         'dependencies' => array(
           'enforced' => array(
-            'config' => array($entity1->getConfigDependencyName()),
+            'config' => array($entity_d->getConfigDependencyName()),
             'module' => array('node', 'config_test')
           ),
         ),
       )
     );
-    $entity4->save();
+    $entity_a->save();
+
+    // Set a more complicated test where dependencies will be fixed.
+    \Drupal::state()->set('config_test.fix_dependencies', array($entity_d->getConfigDependencyName()));
+    \Drupal::state()->set('config_test.on_dependency_removal_called', []);
 
     // Do a dry run using
     // \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().
     $config_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval('module', ['node']);
-    $this->assertEqual($entity1->uuid(), $config_entities['delete'][0]->uuid(), 'Entity 1 will be deleted.');
-    $this->assertEqual($entity2->uuid(), reset($config_entities['update'])->uuid(), 'Entity 2 will be updated.');
-    $this->assertEqual($entity3->uuid(), reset($config_entities['unchanged'])->uuid(), 'Entity 3 is not changed.');
-    $this->assertEqual($entity4->uuid(), $config_entities['delete'][1]->uuid(), 'Entity 4 will be deleted.');
+    $this->assertEqual($entity_d->uuid(), $config_entities['delete'][0]->uuid(), 'Entity D will be deleted.');
+    $this->assertEqual($entity_c->uuid(), reset($config_entities['update'])->uuid(), 'Entity C will be updated.');
+    $this->assertEqual($entity_b->uuid(), reset($config_entities['unchanged'])->uuid(), 'Entity B is not changed.');
+    $this->assertEqual($entity_a->uuid(), $config_entities['delete'][1]->uuid(), 'Entity A will be deleted.');
 
     $called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
-    $this->assertFalse(in_array($entity3->id(), $called), 'ConfigEntityInterface::onDependencyRemoval() is not called for entity 3.');
-    $this->assertIdentical(['entity1', 'entity2', 'entity4'], $called, 'The most dependent entites have ConfigEntityInterface::onDependencyRemoval() called first.');
+    $this->assertFalse(in_array($entity_b->id(), $called), 'ConfigEntityInterface::onDependencyRemoval() is not called for entity B.');
+    $this->assertIdentical(['entity_d', 'entity_a', 'entity_c'], $called, 'The most dependent entites have ConfigEntityInterface::onDependencyRemoval() called first.');
 
     // Perform the uninstall.
     $config_manager->uninstall('module', 'node');
 
+    $storage->resetCache();
     // Test that expected actions have been performed.
-    $this->assertFalse($storage->load('entity1'), 'Entity 1 deleted');
-    $entity2 = $storage->load('entity2');
-    $this->assertTrue($entity2, 'Entity 2 not deleted');
-    $this->assertEqual($entity2->calculateDependencies()->getDependencies()['config'], array(), 'Entity 2 dependencies updated to remove dependency on Entity1.');
-    $entity3 = $storage->load('entity3');
-    $this->assertTrue($entity3, 'Entity 3 not deleted');
-    $this->assertEqual($entity3->calculateDependencies()->getDependencies()['config'], [$entity2->getConfigDependencyName()], 'Entity 3 still depends on Entity 2.');
-    $this->assertFalse($storage->load('entity4'), 'Entity 4 deleted');
+    $this->assertFalse($storage->load('entity_d'), 'Entity D deleted');
+    $entity_c = $storage->load('entity_c');
+    $this->assertTrue($entity_c, 'Entity C not deleted');
+    $this->assertEqual($entity_c->calculateDependencies()->getDependencies()['config'], array(), 'Entity C dependencies updated to remove dependency on Entity D.');
+    $entity_b = $storage->load('entity_b');
+    $this->assertTrue($entity_b, 'Entity B not deleted');
+    $this->assertEqual($entity_b->calculateDependencies()->getDependencies()['config'], [$entity_c->getConfigDependencyName()], 'Entity B still depends on Entity C.');
+    $this->assertFalse($storage->load('entity_a'), 'Entity A deleted');
   }
 
   /**
