--- nodequeue.module	2010-03-16 15:10:45.000000000 -0700
+++ nodequeue.module.patched	2010-04-13 11:49:15.000000000 -0700
@@ -558,8 +558,23 @@ function nodequeue_node_tab($node) {
  */
 function nodequeue_view_queues() {
   $output = theme('advanced_help_topic', 'nodequeue', 'about', 'icon') . '&nbsp;' . theme('advanced_help_topic', 'nodequeue', 'about', t('Click here for information about this module'));
+  
+  $header = array(
+    array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
+    array('data' => t('Max nodes'), 'field' => 'size'),
+    array('data' => t('Subqueues'), 'field' => 'subqueues'),
+    array('data' => t('Operation')),
+  );
+  $table_sort = tablesort_init($header);
+  
+  $sort = array('field' => 'qid', 'direction' => 'asc');
+  if ($table_sort) {
+    $sort['field'] = $table_sort['sql'];
+    $sort['direction'] = $table_sort['sort'];
+  }
+  
   // Fetch all of the queues.
-  $queues = nodequeue_load_queues(nodequeue_get_all_qids(25));
+  $queues = nodequeue_load_queues(nodequeue_get_all_qids(25, 0, FALSE, $sort));
   foreach ($queues as $queue) {
     if (!nodequeue_queue_access($queue)) {
       unset($queues[$queue->qid]);
@@ -570,14 +585,6 @@ function nodequeue_view_queues() {
     return $output . t('No nodequeues exist.');
   }
 
-  $header = array(
-    array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
-    array('data' => t('Max nodes'), 'field' => 'size'),
-    array('data' => t('Subqueues'), 'field' => 'subqueues'),
-    array('data' => t('Operation')),
-  );
-  $table_sort = tablesort_init($header);
-
   $qids = array();
   $sort_primary = array();
   $sort_secondary = array();
@@ -1615,15 +1622,25 @@ function nodequeue_get_qids($type, $acco
  * @param $bypass_cache
  *   Boolean value indicating whether to bypass the cache or not.
  */
-function nodequeue_get_all_qids($page_size = 25, $pager_element = 0, $bypass_cache = FALSE) {
+function nodequeue_get_all_qids($page_size = 25, $pager_element = 0, $bypass_cache = FALSE, $sort = array()) {
   static $cache = NULL;
+  
+  $accepted_sorts = array('qid', 'title', 'size');
+  $accepted_sort_directions = array('asc', 'desc');  
+  $sort_field = (in_array($sort['field'], $accepted_sorts) &&
+    in_array($sort['direction'], $accepted_sort_directions)) ? implode(' ', $sort) : '';
+  
   if ($bypass_cache || !isset($cache)) {
     $sql = 'SELECT nq.qid '.
            'FROM {nodequeue_queue} nq '.
            'WHERE nq.show_in_ui = 1 ';
     $count_sql = 'SELECT COUNT(q.qid) FROM {nodequeue_queue} q WHERE q.show_in_ui = 1 ';
+    if ($sort_field) {
+      $sql .= 'ORDER BY %s';
+    }
+        
     if ($page_size) {
-      $result = pager_query($sql, $page_size, $pager_element, $count_sql);
+      $result = pager_query($sql, $page_size, $pager_element, $count_sql, $sort_field);
     }
     else {
       $result = db_query($sql, $count_sql);
