Index: trunk/roundcubemail/program/include/rcube_imap.inc
===================================================================
--- trunk/roundcubemail/program/include/rcube_imap.inc	(revision 288)
+++ trunk/roundcubemail/program/include/rcube_imap.inc	(revision 291)
@@ -517,13 +517,15 @@
       $headers_sorted = TRUE;
       }
+    // cache is dirty, sync it
+    else if ($this->caching_enabled && $cache_status==-1 && !$recursive)
+      {
+      $this->sync_header_index($mailbox);
+      return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE);
+      }
     else
       {
-      $sorter = new rcube_header_sorter();
-
       // retrieve headers from IMAP
       if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
-        {
-        $sorter->set_sequence_numbers($msg_index);
-        
+        {        
         $msgs = $msg_index[$begin];
         for ($i=$begin+1; $i < $end; $i++)
@@ -532,15 +534,14 @@
       else
         {
-        $msgs = sprintf("%d:%d", $begin+1, $end);        
-        }
-
-
-      // cache is dirty, sync it
-      if ($this->caching_enabled && $cache_status==-1 && !$recursive)
-        {
-        $this->sync_header_index($mailbox);
-        return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE);
-        }      
-
+        $msgs = sprintf("%d:%d", $begin+1, $end);
+
+        $i = 0;
+        for ($msg_seqnum = $begin; $msg_seqnum <= $end; $msg_seqnum++)
+          $msg_index[$i++] = $msg_seqnum;
+        }
+
+      // use this class for message sorting
+      $sorter = new rcube_header_sorter();
+      $sorter->set_sequence_numbers($msg_index);
 
       // fetch reuested headers from server
@@ -574,14 +575,4 @@
     return array_values($a_msg_headers);
     }
-
-
-
-
-
-function gethdrids($hdr)
-{
-   return $hdr->uid . ',' . $hdr->id;
-}
-
 
 
