diff --git a/core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php b/core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php
index ea8bff10d50b07a59019dedfd698c04da649666f..d87fdf1375c347e3cd3e507059bb8f838cc5eb6f 100644
--- a/core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php
+++ b/core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php
@@ -127,6 +127,9 @@ public static function setLinkActiveClass($html_markup, $current_path, $is_front
     $search_key_current_path = 'data-drupal-link-system-path="' . $current_path . '"';
     $search_key_front = 'data-drupal-link-system-path="<front>"';
 
+    // Receive the query in a standardized manner.
+    ksort($query);
+
     $offset = 0;
     // There are two distinct conditions that can make a link be marked active:
     // 1. A link has the current path in its 'data-drupal-link-system-path'
diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
index 4d3d5075b218790a6b815e25821ea74ddeabd093..8186b006080cbe8b7ce4548bdcf254d1839cded4 100644
--- a/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
+++ b/core/tests/Drupal/Tests/Core/EventSubscriber/ActiveLinkResponseFilterTest.php
@@ -238,6 +238,19 @@ public function providerTestSetLinkActiveClass() {
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => ""]];
     $situations[] = ['context' => $context, 'is active' => FALSE, 'attributes' => $attributes + ['hreflang' => 'en', 'data-drupal-link-query' => TRUE]];
 
+    // Query with unsorted keys must match when the attribute is in sorted form.
+    $context = [
+      'path' => 'myfrontpage',
+      'front' => TRUE,
+      'language' => 'en',
+      'query' => ['foo' => 'bar', 'baz' => 'qux'],
+    ];
+    $attributes = [
+      'data-drupal-link-system-path' => 'myfrontpage',
+      'data-drupal-link-query' => Json::encode(['baz' => 'qux', 'foo' => 'bar']),
+    ];
+    $situations[] = ['context' => $context, 'is active' => TRUE, 'attributes' => $attributes];
+
     // Loop over the surrounding HTML variations.
     $data = [];
     for ($h = 0; $h < count($html); $h++) {