Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.60
diff -u -p -r1.60 tablesort.inc
--- includes/tablesort.inc	28 Oct 2010 02:27:08 -0000	1.60
+++ includes/tablesort.inc	9 Dec 2010 03:49:20 -0000
@@ -237,30 +237,29 @@ function tablesort_get_query_parameters(
  *   - "sql": The name of the database field to sort on.
  */
 function tablesort_get_order($headers) {
+  // Determine if the column to order by is in $_GET.
   $order = isset($_GET['order']) ? $_GET['order'] : '';
   foreach ($headers as $header) {
     if (isset($header['data']) && $order == $header['data']) {
       return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
     }
+  }
 
+  // If the 'sort' key is defined in the header, respect the first instance.
+  foreach ($headers as $header) {
     if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
-      $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
+      return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
     }
   }
 
-  if (isset($default)) {
-    return $default;
+  // Otherwise, default to ordering by the first column.
+  $first = current($headers);
+  if (is_array($first)) {
+    $first += array('data' => NULL, 'field' => NULL);
+    return array('name' => $first['data'], 'sql' => $first['field']);
   }
   else {
-    // The first column specified is the initial 'order by' field unless otherwise specified.
-    $first = current($headers);
-    if (is_array($first)) {
-      $first += array('data' => NULL, 'field' => NULL);
-      return array('name' => $first['data'], 'sql' => $first['field']);
-    }
-    else {
-      return array('name' => $first, 'sql' => '');
-    }
+    return array('name' => $first, 'sql' => '');
   }
 }
 
