[PATCH] Sort message UID list

Dave Abrahams dave at boostpro.com
Sun Jul 8 22:12:17 BST 2012

on Sat Jul 07 2012, corvus-AT-gnu.org (James E. Blair) wrote:

> Return a sorted list of UIDs in getmessageuidlist.
> Some MUAs (and their users) like to display messages in UID order
> (or "order received").  If offlineimap is used in IMAP<->IMAP mode,
> then the order messages are received by the second IMAP server will
> be different because offlineimap iterates over a UID list produced
> from the keys of a dictionary, which is unsorted.
> This change sorts that list of UIDs so that both IMAP servers will
> have their messages in the same order (except those times where
> messages are appended to folders on both repositories between
> syncs).
> Signed-off-by: James E. Blair <corvus at gnu.org>
> ---
>  offlineimap/folder/Base.py |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
> index 6f6f364..1052552 100644
> --- a/offlineimap/folder/Base.py
> +++ b/offlineimap/folder/Base.py
> @@ -181,7 +181,7 @@ class BaseFolder(object):
>      def getmessageuidlist(self):
>          """Gets a list of UIDs.
>          You may have to call cachemessagelist() before calling this function!"""
> -        return self.getmessagelist().keys()
> +        return sorted(self.getmessagelist().keys())
>      def getmessagecount(self):
>          """Gets the number of messages."""

Just a couple of remarks:

a. when the list is very long, you could save a lot of CPU/memory by
   sorting in place:

     uids = self.getmessagelist().keys()
     return uids

b. on the other hand, this might turn out to be even better:

     return sorted(self.getmessagelist())

   it's certainly terser.

Dave Abrahams
BoostPro Computing

More information about the OfflineIMAP-project mailing list