Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.842
diff -u -p -r1.842 common.inc
--- includes/common.inc	5 Jan 2009 22:23:58 -0000	1.842
+++ includes/common.inc	6 Jan 2009 02:57:35 -0000
@@ -3184,13 +3184,25 @@ function drupal_alter($type, &$data) {
  *
  * @param $elements
  *   The structured array describing the data to be rendered.
+ * @param $sort
+ *   (optional) Whether the elements should be sorted. This parameter is applied
+ *   recursively; if you need more fine-grained control, set the property
+ *   #sorted on the element level instead, e.g.
+ *   @code
+ *     $element['#sorted'] = TRUE
+ *   @endcode
+ *   $sort should be set to FALSE for elements which are sorted elsewhere, e.g.
+ *   in a database query.
  * @return
  *   The rendered HTML.
  */
-function drupal_render(&$elements) {
-  if (!isset($elements) || (isset($elements['#access']) && !$elements['#access'])) {
+function drupal_render(&$elements, $sort = TRUE) {
+  // Early-return nothing if elements are either rendered already or user does
+  // not have access.
+  if (!isset($elements) || isset($elements['#rendered']) || (isset($elements['#access']) && !$elements['#access'])) {
     return NULL;
   }
+  $elements['#rendered'] = TRUE;
 
   // If the default values for this element haven't been loaded yet, populate
   // them.
@@ -3211,15 +3223,19 @@ function drupal_render(&$elements) {
     }
   }
 
-  $content = '';
-  // Either the elements did not go through form_builder or one of the children
-  // has a #weight.
-  if (!isset($elements['#sorted'])) {
-    uasort($elements, "element_sort");
+  // Sort the elements by weight, if there are any children, they have not been
+  // rendered by form_builder() already, and $sort was not explicitly set to
+  // FALSE.
+  $children = !isset($elements['#children']) ? element_children($elements) : FALSE;
+  $sort = isset($elements['#sorted']) ? FALSE : $sort;
+  if ($sort && $children) {
+    uasort($elements, 'element_sort');
+    $elements['#sorted'] = TRUE;
   }
+
+  $content = '';
   $elements += array('#title' => NULL, '#description' => NULL);
   if (!isset($elements['#children'])) {
-    $children = element_children($elements);
     // Render all the children that use a theme function.
     if (isset($elements['#theme']) && empty($elements['#theme_used'])) {
       $elements['#theme_used'] = TRUE;
@@ -3246,7 +3262,7 @@ function drupal_render(&$elements) {
     // Render each of the children using drupal_render and concatenate them.
     if (!isset($content) || $content === '') {
       foreach ($children as $key) {
-        $content .= drupal_render($elements[$key]);
+        $content .= drupal_render($elements[$key], $sort);
       }
     }
   }
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.460
diff -u -p -r1.460 theme.inc
--- includes/theme.inc	4 Jan 2009 16:19:39 -0000	1.460
+++ includes/theme.inc	6 Jan 2009 02:21:23 -0000
@@ -1988,13 +1988,13 @@ function template_preprocess_node(&$vari
   if ($node->build_mode == NODE_BUILD_PREVIEW) {
     unset($node->content['links']);
   }
-  
+
   // Render taxonomy links separately.
   $variables['terms']     = !empty($node->content['links']['terms']) ? drupal_render($node->content['links']['terms']) : '';
-  
+
   // Render all remaining node links.
   $variables['links']     = !empty($node->content['links']) ? drupal_render($node->content['links']) : '';
-  
+
   // Render any comments.
   $variables['comments']  = !empty($node->content['comments']) ? drupal_render($node->content['comments']) : '';
 
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.680
diff -u -p -r1.680 comment.module
--- modules/comment/comment.module	4 Jan 2009 16:19:39 -0000	1.680
+++ modules/comment/comment.module	6 Jan 2009 02:21:23 -0000
@@ -498,6 +498,7 @@ function comment_nodeapi_view($node, $te
     if ($node->comment && (bool)menu_get_object() && $node->build_mode != NODE_BUILD_PREVIEW) {
       $node->content['comments'] = array(
         '#markup' => comment_render($node),
+        '#sorted' => TRUE,
       );
     }
   }
Index: modules/taxonomy/taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.451
diff -u -p -r1.451 taxonomy.module
--- modules/taxonomy/taxonomy.module	30 Dec 2008 16:43:19 -0000	1.451
+++ modules/taxonomy/taxonomy.module	6 Jan 2009 02:21:23 -0000
@@ -76,7 +76,8 @@ function taxonomy_nodeapi_view($node) {
   
   $node->content['links']['terms'] = array(
     '#type' => 'node_links',
-    '#value' => $links
+    '#value' => $links,
+    '#sorted' => TRUE,
   );
 }
 
