diff --git a/core/lib/Drupal/Core/Theme/Registry.php b/core/lib/Drupal/Core/Theme/Registry.php
index 29b6e39..6fb3733 100644
--- a/core/lib/Drupal/Core/Theme/Registry.php
+++ b/core/lib/Drupal/Core/Theme/Registry.php
@@ -669,8 +669,9 @@ protected function postProcessExtension(array &$cache, ActiveTheme $theme) {
     // will go missing. This will add the expected function. It also allows
     // modules or themes to have a variable process function based on a pattern
     // even if the hook does not exist.
-    for ($level = 1; $level <= count($suggestion_level); $level++) {
-      foreach ($suggestion_level[$level] as $preprocessor => $hook) {
+    ksort($suggestion_level);
+    foreach ($suggestion_level as $level => $item) {
+      foreach ($item as $preprocessor => $hook) {
         if (isset($cache[$hook]['preprocess functions']) && !in_array($hook, $cache[$hook]['preprocess functions'])) {
           // Add missing preprocessor to existing hook.
           $cache[$hook]['preprocess functions'][] = $preprocessor;
diff --git a/core/modules/system/src/Tests/Theme/RegistryTest.php b/core/modules/system/src/Tests/Theme/RegistryTest.php
index e6c80be..1fc3715 100644
--- a/core/modules/system/src/Tests/Theme/RegistryTest.php
+++ b/core/modules/system/src/Tests/Theme/RegistryTest.php
@@ -139,6 +139,7 @@ public function testSuggestionPreprocessFunctions() {
     $preprocess_functions = $registry_theme->get()['theme_test_preprocess_suggestions__kitten__bearcat']['preprocess functions'];
     $this->assertIdentical($expected_preprocess_functions, $preprocess_functions, 'Suggestion implemented as a template correctly inherits preprocess functions.');
 
+    $this->assertTrue(isset($registry_theme->get()['theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose']), 'Preprocess function with an unimplemented lower-level suggestion is added to the registry.');
   }
 
   /**
diff --git a/core/modules/system/tests/themes/test_theme/test_theme.theme b/core/modules/system/tests/themes/test_theme/test_theme.theme
index 3ce72e3..36cb4d5 100644
--- a/core/modules/system/tests/themes/test_theme/test_theme.theme
+++ b/core/modules/system/tests/themes/test_theme/test_theme.theme
@@ -142,3 +142,10 @@ function test_theme_preprocess_theme_test_preprocess_suggestions__kitten(&$varia
 function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__flamingo(&$variables) {
   $variables['bar'] = 'Flamingo';
 }
+
+/**
+ * Tests a preprocess function with suggestions.
+ */
+function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose(&$variables) {
+  $variables['bar'] = 'Moose';
+}
