Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.864
diff -u -p -r1.864 common.inc
--- includes/common.inc	5 Feb 2009 01:21:16 -0000	1.864
+++ includes/common.inc	8 Feb 2009 00:37:39 -0000
@@ -3325,10 +3325,9 @@ function drupal_render(&$elements) {
 
   // Sort the elements by weight if they have not been sorted elsewhere already,
   // for example by a database query. Pre-sorted elements should have
-  // $elements['#sorted'] set to TRUE to avoid unnecessary calls to uasort().
+  // $elements['#sorted'] set to TRUE.
   if (empty($elements['#sorted'])) {
-    uasort($elements, 'element_sort');
-    $elements['#sorted'] = TRUE;
+    $children = drupal_sort_children($elements);
   }
 
   $elements['#children'] = '';
@@ -3340,7 +3339,8 @@ function drupal_render(&$elements) {
   // If #theme was not set and the element has children, render them now
   // using drupal_render_children().
   if ($elements['#children'] == '') {
-    $elements['#children'] = drupal_render_children($elements);
+    $children_keys = isset($children) ? $children : NULL;
+    $elements['#children'] = drupal_render_children($elements, $children_keys);
   }
 
   // Let the theme function in #theme_wrapper add markup around the rendered
@@ -3487,6 +3487,36 @@ function element_children($element) {
 }
 
 /**
+ * Sort the children element being processed by drupal_render().
+ *
+ * @param $elements
+ *   The element to be sorted.
+ *
+ * @return
+ *   A sorted array of the children's keys.
+ */
+function drupal_sort_children(&$element) {
+  // First determine if the element has viable children. This is necessary
+  // to avoid unnecessary calls to uasort().
+  $children = array();
+  $sort = FALSE;
+  foreach ($element as $key => $value) {
+    if ($key[0] !== '#') {
+      $children[$key] = $value;
+      if (is_array($value) && isset($value['#weight'])) {
+        $sort = TRUE;
+      }
+    }
+  }
+  // Sort the element if necessary.
+  if ($sort) {
+    uasort($children, 'element_sort');
+  }
+  $element['#sorted'] = TRUE;
+  return array_keys($children);
+}
+
+/**
  * Provide theme registration for themes across .inc files.
  */
 function drupal_common_theme() {
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.25
diff -u -p -r1.25 common.test
--- modules/simpletest/tests/common.test	31 Jan 2009 19:07:45 -0000	1.25
+++ modules/simpletest/tests/common.test	8 Feb 2009 00:37:39 -0000
@@ -535,8 +535,28 @@ class DrupalRenderUnitTestCase extends D
       ),
     );
     $output = drupal_render($elements);
+
     // The lowest weight element should appear last in $output.
-    $this->assertTrue(strpos($output, $second) > strpos($output, $first), t('Elements were sorted correctly by weight'));
+    $this->assertTrue(strpos($output, $second) > strpos($output, $first), t('Elements were sorted correctly by weight.'));
+
+    // Confirm that the $elements array has '#sorted' set to TRUE.
+    $this->assertTrue($elements['#sorted'], t("'#sorted' => TRUE was added to the array"));
+
+    // Now the same array structure, but with #sorted set to TRUE.
+    $elements = array(
+      'second' => array(
+        '#weight' => 10,
+        '#markup' => $second,
+      ),
+      'first' => array(
+        '#weight' => 0,
+        '#markup' => $first,
+      ),
+      '#sorted' => TRUE,
+    );
+    $output = drupal_render($elements);
+    // The elements should appear in output in the same order as the array.
+    $this->assertTrue(strpos($output, $second) < strpos($output, $first), t('Elements were not sorted.'));
   }
 }
 
