Patch from http://drupal.org/node/1388546 -- http://qa.drupal.org/pifr/test/322903
to fix broken javascript aggregation when the same file is included more than once

Index: ../includes/common.inc
===================================================================
--- ../includes/common.inc	(revision 16192)
+++ ../includes/common.inc	(working copy)
@@ -2913,9 +2913,6 @@
       $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':
@@ -2926,6 +2923,8 @@
       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;
     }
   }
@@ -2973,7 +2972,7 @@
   }
 
   // Sort CSS items, so that they appear in the correct order.
-  uasort($css, 'drupal_sort_css_js');
+  drupal_sort_css_js_stable($css);
 
   // Provide the page with information about the individual CSS files used,
   // information not otherwise available when CSS aggregation is enabled. The
@@ -3015,56 +3014,38 @@
 }
 
 /**
- * Sorts CSS and JavaScript resources.
+ * 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().
  *
- * Callback for uasort() within:
- * - drupal_get_css()
- * - drupal_get_js()
+ * The function will sort by:
+ * - $item['group'],      integer, ascending
+ * - $item['every_page'], boolean, first TRUE then FALSE
+ * - $item['weight'],     integer, ascending
  *
- * 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.
- *
- * @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.
- *
- * @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;
+  $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;
+        }
+      }
+    }
   }
-  // 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;
-  }
+  $items = $sorted;
 }
 
 /**
@@ -4032,10 +4013,6 @@
   // 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)) {
     // Add jquery.js and drupal.js, as well as the basePath setting, the
     // first time a JavaScript file is added.
@@ -4087,6 +4064,8 @@
       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;
     }
   }
@@ -4198,7 +4177,7 @@
   $js_version_string = variable_get('drupal_js_version_query_string', 'v=');
 
   // Sort the JavaScript so that it appears in the correct order.
-  uasort($items, 'drupal_sort_css_js');
+  drupal_sort_css_js_stable($items);
 
   // Provide the page with information about the individual JavaScript files
   // used, information not otherwise available when aggregation is enabled.
