diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php b/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php
index 377f7c4..dd87598 100644
--- a/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php
@@ -173,7 +173,8 @@ 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));
+    $sort = $this->sortAll();
+    return array_intersect_key(array_combine($sort, $sort), $dependencies);
   }
 
   /**
@@ -188,7 +189,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_keys(array_intersect_key($graph, $this->data));
   }
 
   /**
@@ -228,9 +230,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];
+          }
         }
       }
     }
@@ -249,11 +253,10 @@ protected function getGraph() {
       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;
-          }
+        // 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..3152402 100644
--- a/core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php
@@ -293,7 +293,7 @@ public function testConfigEntityUninstall() {
 
     $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->assertIdentical(['entity1', 'entity4', 'entity2'], $called, 'The most dependent entites have ConfigEntityInterface::onDependencyRemoval() called first.');
 
     // Perform the uninstall.
     $config_manager->uninstall('module', 'node');
