--- common.inc	2010-09-27 02:31:35.000000000 +0300
+++ common_fixed.inc	2010-09-30 12:05:41.134389820 +0300
@@ -5454,15 +5454,36 @@ function drupal_render_cid_create($eleme
 }
 
 /**
- * Function used by uasort to sort structured arrays by weight.
+ * Function used to quicksort structured arrays by weight.
  */
-function element_sort($a, $b) {
-  $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0;
-  $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0;
-  if ($a_weight == $b_weight) {
-    return 0;
+function element_sort($elements) {
+  if (count($elements) < 2) {
+    return $elements;
   }
-  return ($a_weight < $b_weight) ? -1 : 1;
+
+  $left = $right = array();
+
+  // Loop only once to get the first element
+  // array_shift() won't work because it resetes our precious keys
+  foreach ($elements as $key => $element) {
+    $pivot_key = $key;
+    $pivot = $element;
+    unset($elements[$key]);
+    break;
+  }
+
+  foreach($elements as $k => $v) {
+    $v_weight = (is_array($v) && isset($v['#weight'])) ? $v['#weight'] : 0;
+    $pivot_weight = (is_array($pivot) && isset($pivot['#weight'])) ? $pivot['#weight'] : 0;
+    if ($v_weight < $pivot_weight) {
+      $left[$k] = $v;
+    }
+    else {
+      $right[$k] = $v;
+    }
+  }
+
+  return array_merge(element_sort($left), array($pivot_key => $pivot), element_sort($right));
 }
 
 /**
@@ -5557,7 +5578,7 @@ function element_children(&$elements, $s
   }
   // Sort the children if necessary.
   if ($sort && $sortable) {
-    uasort($children, 'element_sort');
+    $children = element_quicksort($children);
     // Put the sorted children back into $elements in the correct order, to
     // preserve sorting if the same element is passed through
     // element_children() twice.
