Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.841
diff -u -p -r1.841 common.inc
--- includes/common.inc	30 Dec 2008 16:43:14 -0000	1.841
+++ includes/common.inc	5 Jan 2009 00:18:54 -0000
@@ -3184,13 +3184,16 @@ function drupal_alter($type, &$data) {
  *
  * @param $elements
  *   The structured array describing the data to be rendered.
+ * @param $sort
+ *   Boolean to determine if the elements should be sorted by drupal_render().
  * @return
  *   The rendered HTML.
  */
-function drupal_render(&$elements) {
-  if (!isset($elements) || (isset($elements['#access']) && !$elements['#access'])) {
+function drupal_render(&$elements, $sort = TRUE) {
+  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.
@@ -3212,14 +3215,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");
-  }
+  $children = !isset($elements['#children']) ? element_children($elements) : FALSE;
+  // Sort the elements by weight. This should be done if the elements did not
+  // go through form_builder, if the element has children, and if we have not 
+  // explicitly set $sort to FALSE for elements which are sorted elsewhere,
+  // i.e. via a database query.
+  $sort = isset($elements['#sorted']) ? FALSE : $sort;
+    if ($sort && $children && !isset($elements['#markup'])) {
+      uasort($elements, 'element_sort');
+      $elements['#sorted'] = TRUE;
+    }
+
   $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 +3254,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	5 Jan 2009 00:18:55 -0000
@@ -1988,15 +1988,16 @@ 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']) : '';
-  
+  // Set #sorted and #children to avoid calling uasort and array_filter().
+  $variables['terms']     = !empty($node->content['links']['terms']) ? drupal_render($node->content['links']['terms'], FALSE) : '';
+
   // Render all remaining node links.
-  $variables['links']     = !empty($node->content['links']) ? drupal_render($node->content['links']) : '';
-  
+  $variables['links']     = !empty($node->content['links']) ? drupal_render($node->content['links'], FALSE) : '';
+
   // Render any comments.
-  $variables['comments']  = !empty($node->content['comments']) ? drupal_render($node->content['comments']) : '';
+  $variables['comments']  = !empty($node->content['comments']) ? drupal_render($node->content['comments'], FALSE) : '';
 
   // Render the rest of the node into $content.
   if (!empty($node->content['teaser'])) {
