diff --git a/include/class.forms.php b/include/class.forms.php
index 7e0b829199f65e40ce4b8ebb9ae53a50d1eafc3a..6e3e39141d92bae69ddf30149be8c67ec2a16907 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -1054,10 +1054,18 @@ class FormField {
 
     function getLabel() { return $this->get('label'); }
 
+    function getSortKeys($path) {
+        return array($path);
+    }
+
+    function getOrmPath($name=false, $query=null) {
+        return CustomQueue::getOrmPath($name ?:$this->get('name'), $query);
+    }
+
     function applyOrderBy($query, $reverse=false, $name=false) {
-        $col = $name ?: CustomQueue::getOrmPath($this->get('name'), $query);
-        if ($reverse)
-            $col = '-' . $col;
+        $col = sprintf('%s%s',
+                $reverse ? '-' : '',
+                $this->getOrmPath($name, $query));
         return $query->order_by($col);
     }
 
diff --git a/include/class.queue.php b/include/class.queue.php
index c51aa79a69d51246969a4fde16716e9177e494ac..a37accc7abb9b9b42fdf25e01da405b4cf8fc64e 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -2097,7 +2097,8 @@ extends VerySimpleModel {
             $fields = array();
             foreach ($keys as $key) {
                 list($path, $field) = $key;
-                $fields[] = new SqlField($path);
+                foreach ($field->getSortKeys($path) as $field)
+                    $fields[]  = new SqlField($field);
             }
             // Force nulls to the buttom.
             $fields[] = 'zzz';
diff --git a/include/class.search.php b/include/class.search.php
index 8b6ec005915d1055f1543ec0526363f34d163b9a..6ad68f283dd04b2b835fde67935c464821ca8a89 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -1015,7 +1015,7 @@ class DepartmentChoiceField extends AdvancedSearchSelectionField {
         return Dept::getDepartments();
     }
 
-   function getQuickFilterChoices() {
+    function getQuickFilterChoices() {
        global $thisstaff;
 
        if (!isset($this->_choices)) {
@@ -1028,7 +1028,7 @@ class DepartmentChoiceField extends AdvancedSearchSelectionField {
        }
 
        return $this->_choices;
-   }
+    }
 
     function getSearchMethods() {
         return array(
@@ -1036,6 +1036,15 @@ class DepartmentChoiceField extends AdvancedSearchSelectionField {
             '!includes' =>  __('is not'),
         );
     }
+
+    function addToQuery($query, $name=false) {
+        return $query->values('dept_id', 'dept__name');
+    }
+
+    function applyOrderBy($query, $reverse=false, $name=false) {
+        $reverse = $reverse ? '-' : '';
+        return $query->order_by("{$reverse}dept__name");
+    }
 }
 
 
@@ -1136,6 +1145,14 @@ class AssigneeChoiceField extends ChoiceField {
     }
 
     function addToQuery($query, $name=false) {
+
+        $fields = array();
+        foreach(Staff::getsortby('staff__') as $key)
+             $fields[] = new SqlField($key);
+        $fields[] =  new SqlField('team__name');
+        $fields[] = 'zzz';
+        $expr = call_user_func_array(array('SqlFunction', 'COALESCE'), $fields);
+        $query->annotate(array($name ?: 'assignee' => $expr));
         return $query->values('staff__firstname', 'staff__lastname', 'team__name', 'team_id');
     }
 
@@ -1144,16 +1161,12 @@ class AssigneeChoiceField extends ChoiceField {
             return new AgentsName(array('first' => $row['staff__firstname'], 'last' => $row['staff__lastname']));
         if ($row['team_id'])
             return Team::getLocalById($row['team_id'], 'name', $row['team__name']);
+
     }
 
     function display($value) {
         return (string) $value;
     }
-
-    function applyOrderBy($query, $reverse=false, $name=false) {
-        $reverse = $reverse ? '-' : '';
-        return Staff::nsort($query, $reverse);
-    }
 }
 
 class AssignedField extends AssigneeChoiceField {
@@ -1225,10 +1238,13 @@ class AgentSelectionField extends AdvancedSearchSelectionField {
         return parent::getSearchQ($method, $value, $name);
     }
 
+    function getSortKeys($path='') {
+        return Staff::getsortby('staff__');
+    }
 
     function applyOrderBy($query, $reverse=false, $name=false) {
         $reverse = $reverse ? '-' : '';
-        return Staff::nsort($query, $reverse);
+        return Staff::nsort($query, "{$reverse}staff__");
     }
 }
 
@@ -1262,6 +1278,10 @@ class TeamSelectionField extends AdvancedSearchSelectionField {
         return parent::getSearchQ($method, $value, $name);
     }
 
+    function getSortKeys() {
+        return array('team__name');
+    }
+
     function applyOrderBy($query, $reverse=false, $name=false) {
         $reverse = $reverse ? '-' : '';
         return $query->order_by("{$reverse}team__name");
diff --git a/include/class.staff.php b/include/class.staff.php
index 0c35a0c1c4b5fb33f9b327a811d68d4aa624e4d4..5e55d318043f4aef433dcba3988451d68197ed30 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -845,7 +845,7 @@ implements AuthenticatedUser, EmailContact, TemplateVariable, Searchable {
         return self::getStaffMembers(array('available'=>true));
     }
 
-    static function nsort(QuerySet $qs, $path='', $format=null) {
+    static function getsortby($path='', $format=null) {
         global $cfg;
 
         $format = $format ?: $cfg->getAgentNameFormat();
@@ -853,12 +853,18 @@ implements AuthenticatedUser, EmailContact, TemplateVariable, Searchable {
         case 'last':
         case 'lastfirst':
         case 'legal':
-            $qs->order_by("{$path}lastname", "{$path}firstname");
+            $fields = array("{$path}lastname", "{$path}firstname");
             break;
         default:
-            $qs->order_by("${path}firstname", "${path}lastname");
+            $fields = array("${path}firstname", "${path}lastname");
         }
 
+        return $fields;
+    }
+
+    static function nsort(QuerySet $qs, $path='', $format=null) {
+        $fields = self::getsortby($path, $format);
+        $qs->order_by($fields);
         return $qs;
     }