Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.47
diff -u -r1.47 tablesort.inc
--- includes/tablesort.inc	4 Jan 2008 09:31:48 -0000	1.47
+++ includes/tablesort.inc	8 Feb 2008 20:53:33 -0000
@@ -30,23 +30,42 @@
  *   An array of column headers in the format described in theme_table().
  * @param $before
  *   An SQL string to insert after ORDER BY and before the table sorting code.
- *   Useful for sorting by important attributes like "sticky" first.
+ *   Useful for sorting on a discrete variable (e.g. sticky or uid)
+ *   _before_ tablesorting on a continuous variable is performed.
+ * @param $after
+ *   An SQL string to insert after the table sorting code.
+ *   Useful for some sorting on a continuous variable (e.g. a date field)
+ *   _after_ tablesorting on a discrete variable has been performed.
  * @return
  *   An SQL string to append to the end of a query.
  *
  * @ingroup database
  */
-function tablesort_sql($header, $before = '') {
+function tablesort_sql($header, $before = '', $after = '') {
+  $sort = array();
+
+  if ($before) {
+    $sort[] = $before;
+  }
+
   $ts = tablesort_init($header);
   if ($ts['sql']) {
     // Based on code from db_escape_table(), but this can also contain a dot.
     $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
 
     // Sort order can only be ASC or DESC.
-    $sort = drupal_strtoupper($ts['sort']);
-    $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
+    $order = drupal_strtoupper($ts['sort']);
+    $order = in_array($order, array('ASC', 'DESC')) ? $order : '';
+
+    $sort[] = "$field $order";
+  }
+
+  if ($after) {
+    $sort[] = $after;
+  }
 
-    return " ORDER BY $before $field $sort";
+  if (!empty($sort)) {
+    return " ORDER BY " . implode(', ', $sort);
   }
 }
 
