diff --git a/core/lib/Drupal/Core/Render/Element.php b/core/lib/Drupal/Core/Render/Element.php
index eed5c30..0e1c50d 100644
--- a/core/lib/Drupal/Core/Render/Element.php
+++ b/core/lib/Drupal/Core/Render/Element.php
@@ -77,15 +77,29 @@ public static function children(array &$elements, $sort = FALSE) {
     $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
 
     // Filter out properties from the element, leaving only children.
-    $children = array();
+    $count = count($elements);
+    $child_weights = array();
+    $i = 0;
     $sortable = FALSE;
     foreach ($elements as $key => $value) {
       if ($key === '' || $key[0] !== '#') {
         if (is_array($value)) {
-          $children[$key] = $value;
           if (isset($value['#weight'])) {
+            if (!is_int($value['#weight'])) {
+                trigger_error(
+                    String::format('"@key" has an invalid weight. Only integer values are accepted.',
+                    array('@key' => $key)), E_USER_ERROR
+                );
+            }
+            $child_weights[$key] = $value['#weight'];
             $sortable = TRUE;
           }
+          else {
+            $child_weights[$key] = 0;
+          }
+          // Add a small number between 0 and 1 to keep the order of insertion
+          // of equal weight elements.
+          $child_weights[$key] += $i / $count;
         }
         // Only trigger an error if the value is not null.
         // @see http://drupal.org/node/1283892
@@ -93,21 +107,24 @@ public static function children(array &$elements, $sort = FALSE) {
           trigger_error(String::format('"@key" is an invalid render array key', array('@key' => $key)), E_USER_ERROR);
         }
       }
+      $i++;
     }
+
     // Sort the children if necessary.
     if ($sort && $sortable) {
-      uasort($children, 'Drupal\Component\Utility\SortArray::sortByWeightProperty');
+      asort($child_weights);
       // Put the sorted children back into $elements in the correct order, to
       // preserve sorting if the same element is passed through
       // \Drupal\Core\Render\Element::children() twice.
-      foreach ($children as $key => $child) {
+      foreach ($child_weights as $key => $weight) {
+        $value = $elements[$key];
         unset($elements[$key]);
-        $elements[$key] = $child;
+        $elements[$key] = $value;
       }
       $elements['#sorted'] = TRUE;
     }
 
-    return array_keys($children);
+    return array_keys($child_weights);
   }
 
   /**
diff --git a/core/modules/comment/src/Tests/CommentPagerTest.php b/core/modules/comment/src/Tests/CommentPagerTest.php
index ca1c5a6..4104133 100644
--- a/core/modules/comment/src/Tests/CommentPagerTest.php
+++ b/core/modules/comment/src/Tests/CommentPagerTest.php
@@ -337,19 +337,19 @@ function testTwoPagers() {
     $this->assertRaw('Comment 1 on field comment');
     $this->assertRaw('Comment 1 on field comment_2');
     // Navigate to next page of field 1.
-    $this->clickLinkWithXPath('//a[@rel="next"]');
+    $this->clickLinkWithXPath('//section[contains(concat(" ", normalize-space(@class), " "), " field-name-comment ")]//a[@rel="next"]');
     // Check only one pager updated.
     $this->assertRaw('Comment 2 on field comment');
     $this->assertRaw('Comment 1 on field comment_2');
     // Return to page 1.
     $this->drupalGet('node/' . $node->id());
     // Navigate to next page of field 2.
-    $this->clickLinkWithXPath('//a[@rel="next"]', 1);
+    $this->clickLinkWithXPath('//section[contains(concat(" ", normalize-space(@class), " "), " field-name-comment-2 ")]//a[@rel="next"]');
     // Check only one pager updated.
     $this->assertRaw('Comment 1 on field comment');
     $this->assertRaw('Comment 2 on field comment_2');
     // Navigate to next page of field 1.
-    $this->clickLinkWithXPath('//a[@rel="next"]');
+    $this->clickLinkWithXPath('//section[contains(concat(" ", normalize-space(@class), " "), " field-name-comment ")]//a[@rel="next"]');
     // Check only one pager updated.
     $this->assertRaw('Comment 2 on field comment');
     $this->assertRaw('Comment 2 on field comment_2');
diff --git a/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php b/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php
index 33a64a7..a99ea45 100644
--- a/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php
+++ b/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php
@@ -218,7 +218,8 @@ public function testDefaultSearchPageOrdering() {
     $this->drupalGet('search');
     $elements = $this->xpath('//*[contains(@class, :class)]//a', array(':class' => 'tabs primary'));
     $this->assertIdentical((string) $elements[0]['href'], \Drupal::url('search.view_node_search'));
-    $this->assertIdentical((string) $elements[1]['href'], \Drupal::url('search.view_user_search'));
+    $this->assertIdentical((string) $elements[1]['href'], \Drupal::url('search.view_dummy_search_type'));
+    $this->assertIdentical((string) $elements[2]['href'], \Drupal::url('search.view_user_search'));
   }
 
   /**
diff --git a/core/modules/system/src/Tests/Menu/LocalTasksTest.php b/core/modules/system/src/Tests/Menu/LocalTasksTest.php
index 4edf30e..97f33ce 100644
--- a/core/modules/system/src/Tests/Menu/LocalTasksTest.php
+++ b/core/modules/system/src/Tests/Menu/LocalTasksTest.php
@@ -54,8 +54,8 @@ public function testPluginLocalTask() {
     $this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_view'));
     $this->assertLocalTasks([
       ['menu_test.local_task_test_tasks_view', []],
-      ['menu_test.local_task_test_tasks_settings', []],
       ['menu_test.local_task_test_tasks_edit', []],
+      ['menu_test.local_task_test_tasks_settings', []],
     ]);
 
     // Ensure the view tab is active.
diff --git a/core/tests/Drupal/Tests/Core/Render/ElementTest.php b/core/tests/Drupal/Tests/Core/Render/ElementTest.php
index 20ac844..969bc97 100644
--- a/core/tests/Drupal/Tests/Core/Render/ElementTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/ElementTest.php
@@ -89,6 +89,18 @@ public function testChildren() {
 
     $expected = array('child2', 'child1', 'child3');
     $this->assertSame($expected, Element::children($element_no_weight, TRUE));
+
+    // The order of children with same weight should be preserved.
+    $element_mixed_weight = array(
+      'child5' => array('#weight' => 10),
+      'child3' => array('#weight' => -10),
+      'child1' => array(),
+      'child4' => array('#weight' => 10),
+      'child2' => array(),
+    );
+
+    $expected = array('child3', 'child1', 'child2', 'child5', 'child4');
+    $this->assertSame($expected, Element::children($element_mixed_weight, TRUE));
   }
 
   /**
@@ -105,6 +117,41 @@ public function testInvalidChildren() {
   }
 
   /**
+   * Data provider for testInvalidChildrenWeights .
+   *
+   * @return array
+   */
+  public function InvalidChildWeightProvider() {
+    return array(
+      array(0.1),
+      array('invalid weight'),
+      array(new \stdClass())
+    );
+  }
+
+  /**
+   * Test the children() method with invalid parameters.
+   *
+   * @dataProvider InvalidChildWeightProvider
+   * @expectedException \PHPUnit_Framework_Error
+   * @expectedExceptionMessage "invalidChild" has an invalid weight
+   *
+   * $weight
+   *  An invalid element weight sent by data provider.
+   */
+  public function testInvalidChildrenWeights($weight) {
+    if (is_int($weight)) {
+      $this->markTestSkipped('Invalid parameter. $weight should not be an integer.');
+    }
+    $element = array(
+      'invalidChild' => array('#weight' => $weight),
+      'child2' => array('#weight' => 1),
+      'child3' => array()
+    );
+    Element::children($element);
+  }
+
+  /**
    * Tests the children() method with an ignored key/value pair.
    */
   public function testIgnoredChildren() {
