diff --git a/core/includes/tablesort.inc b/core/includes/tablesort.inc
index 3c70b96..ad5f600 100644
--- a/core/includes/tablesort.inc
+++ b/core/includes/tablesort.inc
@@ -45,13 +45,17 @@ class TableSort extends SelectExtender {
   public function orderByHeader(Array $header) {
     $this->header = $header;
     $ts = $this->init();
-    if (!empty($ts['sql'])) {
+    if (!empty($ts['field'])) {
+      $field = isset($ts['query_field']) ? $ts['query_field'] : $ts['field'];
       // Based on code from db_escape_table(), but this can also contain a dot.
-      $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
+      $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $field);
 
       // Sort order can only be ASC or DESC.
       $sort = drupal_strtoupper($ts['sort']);
       $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
+      if (isset($ts['query_reverse']) && $ts['query_reverse'] && strlen($sort) > 0) {
+        $sort = ($sort == 'ASC') ? 'DESC' : 'ASC';
+      }
       $this->orderBy($field, $sort);
     }
     return $this;
@@ -136,7 +140,7 @@ function tablesort_header($cell, $header, $ts) {
   // Special formatting for the currently sorted column header.
   if (is_array($cell) && isset($cell['field'])) {
     $title = t('sort by @s', array('@s' => $cell['data']));
-    if ($cell['data'] == $ts['name']) {
+    if ($cell['field'] == $ts['field']) {
       // aria-sort is a WAI-ARIA property that indicates if items in a table
       // or grid are sorted in ascending or descending order. See
       // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
@@ -150,7 +154,7 @@ function tablesort_header($cell, $header, $ts) {
       $ts['sort'] = 'asc';
       $image = '';
     }
-    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => array_merge($ts['query'], array('sort' => $ts['sort'], 'order' => $cell['data'])), 'html' => TRUE));
+    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => array_merge($ts['query'], array('sort' => $ts['sort'], 'order' => $cell['field'])), 'html' => TRUE));
 
     unset($cell['field'], $cell['sort']);
   }
@@ -174,7 +178,7 @@ function tablesort_header($cell, $header, $ts) {
  *   A properly formatted cell, ready for _theme_table_cell().
  */
 function tablesort_cell($cell, $header, $ts, $i) {
-  if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
+  if (isset($header[$i]['field']) && $header[$i]['field'] == $ts['field']) {
     if (is_array($cell)) {
       $cell['class'][] = 'active';
     }
@@ -210,7 +214,7 @@ function tablesort_get_order($headers) {
   $order = isset($_GET['order']) ? $_GET['order'] : '';
   foreach ($headers as $header) {
     if (is_array($header)) {
-      if (isset($header['data']) && $order == $header['data']) {
+      if (isset($header['field']) && $order == $header['field']) {
         $default = $header;
         break;
       }
@@ -228,8 +232,9 @@ function tablesort_get_order($headers) {
     }
   }
 
-  $default += array('data' => NULL, 'field' => NULL);
-  return array('name' => $default['data'], 'sql' => $default['field']);
+  $default += array('field' => NULL);
+  $default += array('query_field' => $default['field'], 'query_reverse' => FALSE);
+  return array('field' => $default['field'], 'query_field' => $default['query_field'], 'query_reverse' => $default['query_reverse']);
 }
 
 /**
@@ -250,7 +255,7 @@ function tablesort_get_sort($headers) {
     // Find out which header is currently being sorted.
     $ts = tablesort_get_order($headers);
     foreach ($headers as $header) {
-      if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
+      if (is_array($header) && isset($header['field']) && $header['field'] == $ts['field'] && isset($header['sort'])) {
         return $header['sort'];
       }
     }
diff --git a/core/modules/entity/entity.query.inc b/core/modules/entity/entity.query.inc
index fb7ebf0..e687ba7 100644
--- a/core/modules/entity/entity.query.inc
+++ b/core/modules/entity/entity.query.inc
@@ -607,10 +607,10 @@ class EntityFieldQuery {
       }
     }
 
-    $order = tablesort_get_order($headers);
+    $ts = tablesort_get_order($headers);
     $direction = tablesort_get_sort($headers);
     foreach ($headers as $header) {
-      if (is_array($header) && ($header['data'] == $order['name'])) {
+      if (is_array($header) && ($header['field'] == $ts['field'])) {
         if ($header['type'] == 'field') {
           $this->fieldOrderBy($header['specifier']['field'], $header['specifier']['column'], $direction);
         }
