diff --git a/core/includes/common.inc b/core/includes/common.inc
index dece539..ae4caf4 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -2665,9 +2665,6 @@ function drupal_add_css($data = NULL, $options = NULL) {
       $options['preprocess'] = FALSE;
     }
 
-    // Always add a tiny value to the weight, to conserve the insertion order.
-    $options['weight'] += count($css) / 1000;
-
     // Add the data to the CSS array depending on the type.
     switch ($options['type']) {
       case 'inline':
@@ -2678,6 +2675,8 @@ function drupal_add_css($data = NULL, $options = NULL) {
       default:
         // Local and external files must keep their name as the associative key
         // so the same CSS file is not be added twice.
+        // Also, we move the item to the end of the array.
+        unset($css[$data]);
         $css[$data] = $options;
     }
   }
@@ -2725,7 +2724,7 @@ function drupal_get_css($css = NULL, $skip_alter = FALSE) {
   }
 
   // Sort CSS items, so that they appear in the correct order.
-  uasort($css, 'drupal_sort_css_js');
+  drupal_sort_css_js_stable($css);
 
   // Remove the overridden CSS files. Later CSS files override former ones.
   $previous_item = array();
@@ -2754,56 +2753,38 @@ function drupal_get_css($css = NULL, $skip_alter = FALSE) {
 }
 
 /**
- * Sorts CSS and JavaScript resources.
- *
- * Callback for uasort() within:
- * - drupal_get_css()
- * - drupal_get_js()
- *
- * This sort order helps optimize front-end performance while providing modules
- * and themes with the necessary control for ordering the CSS and JavaScript
- * appearing on a page.
+ * Stable sort for css and js items, to preserve the order of items with equal
+ * sort criteria. This function is used in drupal_get_css() and drupal_get_js().
  *
- * @param $a
- *   First item for comparison. The compared items should be associative arrays
- *   of member items from drupal_add_css() or drupal_add_js().
- * @param $b
- *   Second item for comparison.
+ * The function will sort by:
+ * - $item['group'],      integer, ascending
+ * - $item['every_page'], boolean, first TRUE then FALSE
+ * - $item['weight'],     integer, ascending
  *
- * @see drupal_add_css()
- * @see drupal_add_js()
+ * @param array &$items
+ *   Array of js or css items, as in drupal_add_css() and drupal_add_js().
+ *   The array keys can be integers or strings.
+ *   The items themselves are arrays.
  */
-function drupal_sort_css_js($a, $b) {
-  // First order by group, so that, for example, all items in the CSS_SYSTEM
-  // group appear before items in the CSS_DEFAULT group, which appear before
-  // all items in the CSS_THEME group. Modules may create additional groups by
-  // defining their own constants.
-  if ($a['group'] < $b['group']) {
-    return -1;
+function drupal_sort_css_js_stable(&$items) {
+  $nested = array();
+  foreach ($items as $key => $item) {
+    $nested[$item['group']][$item['every_page'] ? 1 : 0][$item['weight']][$key] = $item;
   }
-  elseif ($a['group'] > $b['group']) {
-    return 1;
-  }
-  // Within a group, order all infrequently needed, page-specific files after
-  // common files needed throughout the website. Separating this way allows for
-  // the aggregate file generated for all of the common files to be reused
-  // across a site visit without being cut by a page using a less common file.
-  elseif ($a['every_page'] && !$b['every_page']) {
-    return -1;
-  }
-  elseif (!$a['every_page'] && $b['every_page']) {
-    return 1;
-  }
-  // Finally, order by weight.
-  elseif ($a['weight'] < $b['weight']) {
-    return -1;
-  }
-  elseif ($a['weight'] > $b['weight']) {
-    return 1;
-  }
-  else {
-    return 0;
+  $sorted = array();
+  ksort($nested);
+  foreach ($nested as $group => $group_items) {
+    krsort($group_items);
+    foreach ($group_items as $ep => $ep_items) {
+      ksort($ep_items);
+      foreach ($ep_items as $weight => $weight_items) {
+        foreach ($weight_items as $key => $item) {
+          $sorted[$key] = $item;
+        }
+      }
+    }
   }
+  $items = $sorted;
 }
 
 /**
@@ -3791,9 +3772,6 @@ function drupal_add_js($data = NULL, $options = NULL) {
   // Preprocess can only be set if caching is enabled.
   $options['preprocess'] = $options['cache'] ? $options['preprocess'] : FALSE;
 
-  // Tweak the weight so that files of the same weight are included in the
-  // order of the calls to drupal_add_js().
-  $options['weight'] += count($javascript) / 1000;
   if (isset($data)) {
     switch ($options['type']) {
       case 'setting':
@@ -3833,6 +3811,8 @@ function drupal_add_js($data = NULL, $options = NULL) {
       default: // 'file' and 'external'
         // Local and external files must keep their name as the associative key
         // so the same JavaScript file is not added twice.
+        // Also, we make sure the file is moved to the end of the array.
+        unset($javascript[$options['data']]);
         $javascript[$options['data']] = $options;
     }
   }
@@ -3921,7 +3901,8 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS
 
   if (!empty($items)) {
     // Sort the JavaScript files so that they appear in the correct order.
-    uasort($items, 'drupal_sort_css_js');
+    drupal_sort_css_js_stable($items);
+
     // Don't add settings if there is no other JavaScript on the page, unless
     // this is an AJAX request.
     // @todo Clean up container call.
diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
index 9421682..396ad4f 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php
@@ -223,6 +223,15 @@ function testRenderOverride() {
     $styles = drupal_get_css();
     $this->assert(strpos($styles, $system . '/system.base.css') !== FALSE, t('The overriding CSS file is output.'));
     $this->assert(strpos($styles, $system . '/tests/system.base.css') === FALSE, t('The overridden CSS file is not output.'));
+
+    // This should flip the order once again.
+    drupal_add_css($system . '/system.base.css');
+    drupal_add_css($system . '/tests/system.base.css');
+
+    // The dummy stylesheet should be the only one included.
+    $styles = drupal_get_css();
+    $this->assert(strpos($styles, $system . '/tests/system.base.css') !== FALSE, t('The overriding CSS file is output.'));
+    $this->assert(strpos($styles, $system . '/system.base.css') === FALSE, t('The overridden CSS file is not output.'));
   }
 
   /**
