Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.741
diff -u -r1.741 comment.module
--- modules/comment/comment.module	20 Jul 2009 18:51:32 -0000	1.741
+++ modules/comment/comment.module	21 Jul 2009 05:26:52 -0000
@@ -1128,7 +1128,6 @@
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
         $links = module_invoke_all('link', 'comment', $comment, 1);
         drupal_alter('link', $links, $node);
-
         $output .= theme('comment_view', $comment, $node, $links);
       }
     }
@@ -1155,15 +1154,6 @@
         $query->condition('c.status', COMMENT_PUBLISHED);
         $count_query->condition('c.status', COMMENT_PUBLISHED);
       }
-      if ($mode === COMMENT_MODE_FLAT) {
-        $query->orderBy('c.cid', 'ASC');
-      }
-      else {
-        // See comment above. Analysis reveals that this doesn't cost too
-        // much. It scales much much better than having the whole comment
-        // structure.
-        $query->orderBy('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'ASC');
-      }
 
       $query->setCountQuery($count_query);
       $cids = $query->execute()->fetchCol();
@@ -1171,8 +1161,10 @@
       $divs = 0;
       $num_rows = FALSE;
       $render = '';
-      $comments = comment_load_multiple($cids);
+
+      $comments = comment_load_multiple($cids, array(), $mode === COMMENT_MODE_FLAT ? array(array('order' => 'ASC', 'clause' => 'cid')) : array(array('order' => 'ASC', 'clause' => 'SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))')));
       drupal_add_css(drupal_get_path('module', 'comment') . '/comment.css');
+
       foreach ($comments as $comment) {
         $comment = drupal_unpack($comment);
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
@@ -1263,7 +1255,7 @@
  * @return
  *  An array of comment objects, indexed by comment ID.
  */
-function comment_load_multiple($cids = array(), $conditions = array()) {
+function comment_load_multiple($cids = array(), $conditions = array(), $orderbys = array()) {
   $comments = array();
   if ($cids || $conditions) {
     $query = db_select('comment', 'c');
@@ -1284,6 +1276,13 @@
         $query->condition('c.' . $field, $value);
       }
     }
+    
+    // If the orderby array is populated, add those to the query.
+    if ($orderbys) {
+      foreach ($orderbys as $orderby) {
+        $query->orderBy($orderby['clause'], $orderby['order']);
+      }
+    }
     $comments = $query->execute()->fetchAllAssoc('cid');
   }
 
