 core/modules/filter/filter.module                  |    2 +-
 .../lib/Drupal/filter/Tests/FilterAPITest.php      |   32 +++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module
index 1e858ad..64f9637 100644
--- a/core/modules/filter/filter.module
+++ b/core/modules/filter/filter.module
@@ -401,7 +401,7 @@ function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE,
   $text = str_replace(array("\r\n", "\r"), "\n", $text);
 
   // Get a complete list of filters, ordered properly.
-  $filters = $format->filters();
+  $filters = $format->filters()->sort();
 
   // Give filters the chance to escape HTML-like data such as code or formulas.
   foreach ($filters as $filter) {
diff --git a/core/modules/filter/lib/Drupal/filter/Tests/FilterAPITest.php b/core/modules/filter/lib/Drupal/filter/Tests/FilterAPITest.php
index c36b009..8261a92 100644
--- a/core/modules/filter/lib/Drupal/filter/Tests/FilterAPITest.php
+++ b/core/modules/filter/lib/Drupal/filter/Tests/FilterAPITest.php
@@ -68,7 +68,37 @@ function setUp() {
   /**
    * Tests the ability to apply only a subset of filters.
    */
-  function testCheckMarkup() {
+  function testCheckMarkupFilterOrder() {
+    // Create crazy HTML format.
+    $crazy_format = entity_create('filter_format', array(
+      'format' => 'crazy',
+      'name' => 'Crazy',
+      'weight' => 1,
+      'filters' => array(
+        'filter_html_escape' => array(
+          'weight' => 10,
+          'status' => 1,
+        ),
+        'filter_html' => array(
+          'status' => -10,
+          'settings' => array(
+            'allowed_html' => '<p>',
+          ),
+        ),
+      )
+    ));
+    $crazy_format->save();
+
+    $text = "<p>Llamas are <not> awesome!</p>";
+    $expected_filtered_text = "&lt;p&gt;Llamas are  awesome!&lt;/p&gt;";
+
+    $this->assertIdentical(check_markup($text, 'crazy'), $expected_filtered_text, 'Filters applied in correct order.');
+  }
+
+  /**
+   * Tests the ability to apply only a subset of filters.
+   */
+  function testCheckMarkupFilterSubset() {
     $text = "Text with <marquee>evil content and</marquee> a URL: http://drupal.org!";
     $expected_filtered_text = "Text with evil content and a URL: <a href=\"http://drupal.org\">http://drupal.org</a>!";
     $expected_filter_text_without_html_generators = "Text with evil content and a URL: http://drupal.org!";
