Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.50
diff -u -p -r1.50 tablesort.inc
--- includes/tablesort.inc	22 Feb 2009 16:53:41 -0000	1.50
+++ includes/tablesort.inc	15 Apr 2009 19:08:45 -0000
@@ -23,6 +23,11 @@ class TableSort extends SelectQueryExten
   protected $header = array();
 
   public function execute() {
+    return $this->query->execute();
+  }
+
+  public function orderByHeader(Array $header) {
+    $this->header = $header;
     $ts = $this->init();
     if ($ts['sql']) {
       // Based on code from db_escape_table(), but this can also contain a dot.
@@ -33,11 +38,6 @@ class TableSort extends SelectQueryExten
       $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
       $this->orderBy($field, $sort);
     }
-    return $this->query->execute();
-  }
-
-  public function setHeader(Array $header) {
-    $this->header = $header;
     return $this;
   }
 
Index: modules/comment/comment.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.admin.inc,v
retrieving revision 1.17
diff -u -p -r1.17 comment.admin.inc
--- modules/comment/comment.admin.inc	14 Mar 2009 20:13:26 -0000	1.17
+++ modules/comment/comment.admin.inc	15 Apr 2009 19:08:46 -0000
@@ -77,7 +77,7 @@ function comment_admin_overview($type = 
     ->condition('c.status', $status)
     ->extend('PagerDefault')->extend('TableSort')
     ->limit(50)
-    ->setHeader($header);
+    ->orderByHeader($header);
   $result = $query->execute();
 
 
Index: modules/dblog/dblog.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/dblog/dblog.admin.inc,v
retrieving revision 1.15
diff -u -p -r1.15 dblog.admin.inc
--- modules/dblog/dblog.admin.inc	13 Apr 2009 08:48:58 -0000	1.15
+++ modules/dblog/dblog.admin.inc	15 Apr 2009 19:08:47 -0000
@@ -65,20 +65,18 @@ function dblog_overview() {
     ->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
     ->addField('u', 'name');
   if (!empty($filter['where'])) {
-    //setHeader may not be chainable see Line 138
     $query
       ->where($filter['where'], $filter['args'])
       ->extend('PagerDefault')->extend('TableSort')
       ->limit(50, 0)
-      ->setHeader($header);
+      ->orderByHeader($header);
     $result = $query->execute();
   }
   else {
-    //setHeader may not be chainable see Line 138
     $query
       ->extend('PagerDefault')->extend('TableSort')
       ->limit(50)
-      ->setHeader($header);
+      ->orderByHeader($header);
     $result = $query->execute();
   }
 
@@ -131,7 +129,7 @@ function dblog_top($type) {
     ->groupBy('variables')
     ->extend('PagerDefault')->extend('TableSort')
     ->limit(30);
-  $query = $query->setHeader($header);
+  $query = $query->orderByHeader($header);
   $query->setCountQuery($count_query);
   $result = $query->execute();
 
Index: modules/poll/poll.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/poll/poll.pages.inc,v
retrieving revision 1.15
diff -u -p -r1.15 poll.pages.inc
--- modules/poll/poll.pages.inc	9 Mar 2009 20:36:58 -0000	1.15
+++ modules/poll/poll.pages.inc	15 Apr 2009 19:08:48 -0000
@@ -68,7 +68,7 @@ function poll_votes($node) {
     ->extend('PagerDefault')
     ->limit($votes_per_page)
     ->extend('TableSort')
-    ->setHeader($header)
+    ->orderByHeader($header)
     ->execute()
     ->fetchAllAssoc('hostname');
 
Index: modules/simpletest/tests/database_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.module,v
retrieving revision 1.7
diff -u -p -r1.7 database_test.module
--- modules/simpletest/tests/database_test.module	22 Feb 2009 16:53:41 -0000	1.7
+++ modules/simpletest/tests/database_test.module	15 Apr 2009 19:08:49 -0000
@@ -60,6 +60,10 @@ function database_test_menu() {
     'access callback' => TRUE,
     'page callback' => 'database_test_tablesort',
   );
+  $items['database_test/tablesort_first'] = array(
+    'access callback' => TRUE,
+    'page callback' => 'database_test_tablesort_first',
+  );
 
   return $items;
 }
@@ -146,7 +150,36 @@ function database_test_tablesort() {
   $query
     ->fields('t', array('tid', 'pid', 'task', 'priority'));
   
-  $query = $query->extend('TableSort')->setHeader($header);
+  $query = $query->extend('TableSort')->orderByHeader($header);
+
+  // We need all the results at once to check the sort.
+  $tasks = $query->execute()->fetchAll();
+
+  drupal_json(array(
+    'tasks' => $tasks,
+  ));
+  exit;
+}
+
+/**
+ * Run a tablesort query with a second order_by after and return the results.
+ *
+ * This function does care about the page GET parameter, as set by the
+ * simpletest HTTP call.
+ */
+function database_test_tablesort_first() {
+  $header = array(
+    'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
+    'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
+    'task' => array('data' => t('Task'), 'field' => 'task'),
+    'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
+  );
+
+  $query = db_select('test_task', 't');
+  $query
+    ->fields('t', array('tid', 'pid', 'task', 'priority'));
+  
+  $query = $query->extend('TableSort')->orderByHeader($header)->orderBy('priority');
 
   // We need all the results at once to check the sort.
   $tasks = $query->execute()->fetchAll();
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.48
diff -u -p -r1.48 database_test.test
--- modules/simpletest/tests/database_test.test	1 Apr 2009 20:00:46 -0000	1.48
+++ modules/simpletest/tests/database_test.test	15 Apr 2009 19:08:56 -0000
@@ -1713,6 +1713,32 @@ class DatabaseSelectTableSortDefaultTest
       $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order.'));
     }
   }
+  
+  /**
+   * Confirm that if a tablesort's orderByHeader is called before another orderBy, that the header happens first.
+   *
+   */
+  function testTableSortQueryFirst() {
+    $sorts = array(
+      array('field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'),
+      array('field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'),
+      array('field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'),
+      array('field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'),
+      // more elements here
+
+    );
+
+    foreach ($sorts as $sort) {
+      $this->drupalGet('database_test/tablesort_first/', array('query' => array('order' => $sort['field'], 'sort' => $sort['sort'])));
+      $data = json_decode($this->drupalGetContent());
+
+      $first = array_shift($data->tasks);
+      $last = array_pop($data->tasks);
+
+      $this->assertEqual($first->task, $sort['first'], t('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
+      $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
+    }
+  }
 }
 
 /**
Index: modules/statistics/statistics.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.admin.inc,v
retrieving revision 1.21
diff -u -p -r1.21 statistics.admin.inc
--- modules/statistics/statistics.admin.inc	13 Apr 2009 10:40:13 -0000	1.21
+++ modules/statistics/statistics.admin.inc	15 Apr 2009 19:08:57 -0000
@@ -23,7 +23,7 @@ function statistics_recent_hits() {
     ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
     ->fields('u', array('name'))
     ->limit(30)
-    ->setHeader($header);
+    ->orderByHeader($header);
 
   $result = $query->execute();
   $rows = array();
@@ -66,7 +66,7 @@ function statistics_top_pages() {
     ->fields('accesslog', array('path'))
     ->groupBy('path')
     ->limit(30)
-    ->setHeader($header);
+    ->orderByHeader($header);
 
   $count_query = db_select('accesslog');
   $count_query->addExpression('COUNT(DISTINCT path)');
@@ -114,7 +114,7 @@ function statistics_top_visitors() {
     ->groupBy('u.name')
     ->groupBy('bl.iid')
     ->limit(30)
-    ->setHeader($header);
+    ->orderByHeader($header);
 
   $count_query = db_select('accesslog');
   $count_query->addExpression('COUNT(DISTINCT CONCAT(CAST(uid AS char), hostname))');
@@ -159,7 +159,7 @@ function statistics_top_referrers() {
     ->condition('url', '', '<>')
     ->groupBy('url')
     ->limit(30)
-    ->setHeader($header);
+    ->orderByHeader($header);
 
   $count_query = db_select('accesslog');
   $count_query->addExpression('COUNT(DISTINCT url)');
Index: modules/statistics/statistics.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.pages.inc,v
retrieving revision 1.10
diff -u -p -r1.10 statistics.pages.inc
--- modules/statistics/statistics.pages.inc	13 Apr 2009 10:40:13 -0000	1.10
+++ modules/statistics/statistics.pages.inc	15 Apr 2009 19:08:57 -0000
@@ -23,7 +23,7 @@ function statistics_node_tracker() {
       ->fields('u', array('name'))
       ->condition('path', 'node/' . $node->nid . '%', 'LIKE')
       ->limit(30)
-      ->setHeader($header);
+      ->orderByHeader($header);
 
     $result = $query->execute();
     $rows = array();
@@ -61,7 +61,7 @@ function statistics_user_tracker() {
       ->fields('a', array('aid', 'timestamp', 'path', 'title'))
       ->condition('uid', $account->uid)
       ->limit(30)
-      ->setHeader($header);
+      ->orderByHeader($header);
 
     $result = $query->execute();
     $rows = array();
Index: modules/tracker/tracker.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/tracker/tracker.pages.inc,v
retrieving revision 1.17
diff -u -p -r1.17 tracker.pages.inc
--- modules/tracker/tracker.pages.inc	5 Apr 2009 12:26:12 -0000	1.17
+++ modules/tracker/tracker.pages.inc	15 Apr 2009 19:08:57 -0000
@@ -50,7 +50,7 @@ function tracker_page($account = NULL, $
   $query->distinct();
   $query->extend('PagerDefault')->extend('TableSort')
     ->limit(25, 0)
-    ->setHeader($header);
+    ->orderByHeader($header);
 
   $result = $query->execute();
 
