Index: camel/providers/local/camel-mh-summary.c
===================================================================
--- camel/providers/local/camel-mh-summary.c	(revision 10220)
+++ camel/providers/local/camel-mh-summary.c	(working copy)
@@ -40,6 +40,7 @@
 #include "camel-private.h"
 
 #include "camel-mh-summary.h"
+#include "camel-local-private.h"
 
 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
 
@@ -115,30 +116,6 @@ camel_mh_summary_finalise(CamelObject *o
 	g_free(o->priv);
 }
 
-static int 
-sort_uid_cmp (void *enc, int len1, void * data1, int len2, void *data2)
-{
-	static char *sa1=NULL, *sa2=NULL;
-	static int l1=0, l2=0;
-	int a1, a2;
-
-	if (l1 < len1+1) {
-		sa1 = g_realloc (sa1, len1+1);
-		l1 = len1+1;
-	}
-	if (l2 < len2+1) {
-		sa2 = g_realloc (sa2, len2+1);
-		l2 = len2+1;
-	}
-	strncpy (sa1, data1, len1);sa1[len1] = 0;
-	strncpy (sa2, data2, len2);sa2[len2] = 0;	
-
-	a1 = strtoul (sa1, NULL, 10);
-	a2 = strtoul (sa2, NULL, 10);
-
-	return (a1 < a1) ? -1 : (a1 > a2) ? 1 : 0;
-}
-
 /**
  * camel_mh_summary_new:
  *
@@ -152,7 +129,7 @@ CamelMhSummary	*camel_mh_summary_new(str
 
 	((CamelFolderSummary *)o)->folder = folder;
 	if (folder) {
-		camel_db_set_collate (folder->parent_store->cdb_r, "uid", "mh_uid_sort", (CamelDBCollate)sort_uid_cmp);
+		camel_db_set_collate (folder->parent_store->cdb_r, "uid", "mh_uid_sort", (CamelDBCollate)camel_local_frompos_sort);
 		((CamelFolderSummary *)o)->sort_by = "uid";
 		((CamelFolderSummary *)o)->collate = "mh_uid_sort";
 	}
Index: camel/providers/local/camel-mbox-folder.c
===================================================================
--- camel/providers/local/camel-mbox-folder.c	(revision 10220)
+++ camel/providers/local/camel-mbox-folder.c	(working copy)
@@ -69,6 +69,8 @@ static void mbox_append_message(CamelFol
 static CamelMimeMessage *mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex);
 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
 static char* mbox_get_filename (CamelFolder *folder, const char *uid, CamelException *ex);
+static gint mbox_cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2);
+static void mbox_sort_uids (CamelFolder *folder, GPtrArray *uids);
 
 static void mbox_finalise(CamelObject * object);
 
@@ -86,6 +88,8 @@ camel_mbox_folder_class_init(CamelMboxFo
 	camel_folder_class->append_message = mbox_append_message;
 	camel_folder_class->get_message = mbox_get_message;
 	camel_folder_class->get_filename = mbox_get_filename;
+	camel_folder_class->cmp_uids = mbox_cmp_uids;
+	camel_folder_class->sort_uids = mbox_sort_uids;
 
 	lclass->create_summary = mbox_create_summary;
 	lclass->lock = mbox_lock;
@@ -478,3 +482,40 @@ fail:
 
 	return message;
 }
+
+static gint
+mbox_cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2)
+{
+	CamelMboxMessageInfo *a, *b;
+
+	g_return_val_if_fail (folder != NULL, 0);
+	g_return_val_if_fail (folder->summary != NULL, 0);
+
+	a = (CamelMboxMessageInfo *) camel_folder_summary_uid (folder->summary, uid1);
+	b = (CamelMboxMessageInfo *) camel_folder_summary_uid (folder->summary, uid2);
+
+	g_return_val_if_fail (a != NULL, 0);
+	g_return_val_if_fail (b != NULL, 0);
+
+	return a->frompos < b->frompos ? -1 : a->frompos == b->frompos ? 0 : 1;
+}
+
+static void
+mbox_sort_uids (CamelFolder *folder, GPtrArray *uids)
+{
+	CamelException ex;
+
+	g_return_if_fail (parent_class != NULL);
+	g_return_if_fail (folder != NULL);
+
+	camel_exception_init (&ex);
+
+	camel_folder_summary_ensure_infos_loaded (folder->summary, &ex);
+
+	if (camel_exception_is_set (&ex))
+		g_warning ("%s: %s\n", G_STRFUNC, camel_exception_get_description (&ex));
+
+	camel_exception_clear (&ex);
+
+	CAMEL_FOLDER_CLASS (parent_class)->sort_uids (folder, uids);
+}
Index: camel/providers/local/ChangeLog
===================================================================
--- camel/providers/local/ChangeLog	(revision 10220)
+++ camel/providers/local/ChangeLog	(working copy)
@@ -1,3 +1,17 @@
+2009-04-22  Milan Crha  <mcrha@redhat.com>
+
+	** Part of fix for bug #563954
+
+	* camel-mh-summary.c: (sort_uid_cmp), (camel_mh_summary_new):
+	Little cleanup.
+	* camel-mbox-folder.c: (mbox_cmp_uids), (mbox_sort_uids),
+	(camel_mbox_folder_class_init):
+	* camel-maildir-folder.c: (maildir_cmp_uids),
+	(maildir_sort_uids), (camel_maildir_folder_class_init):
+	Subslass sort and compare uids functions to compare based on the
+	position in the mbox file or a received date for maildir, instead
+	of uid values.
+
 2009-03-01  Srinivasa Ragavan  <sragavan@novell.com>
 
 	* camel/providers/local/camel-local-folder.c: Fix for a deadlock,
Index: camel/providers/local/camel-maildir-folder.c
===================================================================
--- camel/providers/local/camel-maildir-folder.c	(revision 10220)
+++ camel/providers/local/camel-maildir-folder.c	(working copy)
@@ -57,6 +57,8 @@ static CamelLocalSummary *maildir_create
 static void maildir_append_message(CamelFolder * folder, CamelMimeMessage * message, const CamelMessageInfo *info, char **appended_uid, CamelException * ex);
 static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex);
 static char* maildir_get_filename (CamelFolder *folder, const char *uid, CamelException *ex);
+static gint maildir_cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2);
+static void maildir_sort_uids (CamelFolder *folder, GPtrArray *uids);
 
 static void maildir_finalize(CamelObject * object);
 
@@ -104,6 +106,8 @@ static void camel_maildir_folder_class_i
 	camel_folder_class->append_message = maildir_append_message;
 	camel_folder_class->get_message = maildir_get_message;
 	camel_folder_class->get_filename = maildir_get_filename;
+	camel_folder_class->cmp_uids = maildir_cmp_uids;
+	camel_folder_class->sort_uids = maildir_sort_uids;
 
 	lclass->create_summary = maildir_create_summary;
 }
@@ -307,3 +311,44 @@ maildir_get_message(CamelFolder * folder
 
 	return message;
 }
+
+static gint
+maildir_cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2)
+{
+	CamelMessageInfo *a, *b;
+	time_t tma, tmb;
+
+	g_return_val_if_fail (folder != NULL, 0);
+	g_return_val_if_fail (folder->summary != NULL, 0);
+
+	a = camel_folder_summary_uid (folder->summary, uid1);
+	b = camel_folder_summary_uid (folder->summary, uid2);
+
+	g_return_val_if_fail (a != NULL, 0);
+	g_return_val_if_fail (b != NULL, 0);
+
+	tma = camel_message_info_date_received (a);
+	tmb = camel_message_info_date_received (b);
+
+	return tma < tmb ? -1 : tma == tmb ? 0 : 1;
+}
+
+static void
+maildir_sort_uids (CamelFolder *folder, GPtrArray *uids)
+{
+	CamelException ex;
+
+	g_return_if_fail (parent_class != NULL);
+	g_return_if_fail (folder != NULL);
+
+	camel_exception_init (&ex);
+
+	camel_folder_summary_ensure_infos_loaded (folder->summary, &ex);
+
+	if (camel_exception_is_set (&ex))
+		g_warning ("%s: %s\n", G_STRFUNC, camel_exception_get_description (&ex));
+
+	camel_exception_clear (&ex);
+
+	CAMEL_FOLDER_CLASS (parent_class)->sort_uids (folder, uids);
+}
Index: camel/camel-folder.c
===================================================================
--- camel/camel-folder.c	(revision 10220)
+++ camel/camel-folder.c	(working copy)
@@ -91,6 +91,7 @@ static GPtrArray        *get_uids       
 static GPtrArray 	*get_uncached_uids   (CamelFolder *, GPtrArray * uids, CamelException *);
 static void              free_uids           (CamelFolder *folder,
 					      GPtrArray *array);
+static gint cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2);
 static void              sort_uids           (CamelFolder *folder,
 					      GPtrArray *uids);
 static GPtrArray        *get_summary         (CamelFolder *folder);
@@ -155,6 +156,7 @@ camel_folder_class_init (CamelFolderClas
 	camel_folder_class->get_uids = get_uids;
 	camel_folder_class->get_uncached_uids = get_uncached_uids;
 	camel_folder_class->free_uids = free_uids;
+	camel_folder_class->cmp_uids = cmp_uids;
 	camel_folder_class->sort_uids = sort_uids;
 	camel_folder_class->get_summary = get_summary;
 	camel_folder_class->free_summary = free_summary;
@@ -1281,27 +1283,52 @@ camel_folder_get_uncached_uids (CamelFol
 	return CF_CLASS (folder)->get_uncached_uids(folder, uids, ex);
 }
 
+static gint
+cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2)
+{
+	g_return_val_if_fail (uid1 != NULL, 0);
+	g_return_val_if_fail (uid2 != NULL, 0);
 
-static int
-uidcmp (const void *v0, const void *v1)
+	return strtoul (uid1, NULL, 10) - strtoul (uid2, NULL, 10);
+}
+
+/**
+ * camel_folder_cmp_uids:
+ * @folder: a #CamelFolder object
+ * @uid1: The first uid.
+ * @uid2: the second uid.
+ *
+ * Compares two uids. The return value meaning is the same as in any other compare function.
+ *
+ * Note that the default compare function expects a decimal number at the beginning of a uid,
+ * thus if provider uses different uid values, then it should subclass this function.
+ **/
+gint
+camel_folder_cmp_uids (CamelFolder *folder, const char *uid1, const char *uid2)
 {
-	const char *str0 = *(const char **) v0;
-	const char *str1 = *(const char **) v1;
-	guint32 uid0 = strtoul (str0, NULL, 10);
-	guint32 uid1 = strtoul (str1, NULL, 10);
-	
-	if (uid0 < uid1)
-		return -1;
-	else if (uid0 == uid1)
-		return 0;
-	else
-		return 1;
+	g_return_val_if_fail (CAMEL_IS_FOLDER (folder), 0);
+	g_return_val_if_fail (uid1 != NULL, 0);
+	g_return_val_if_fail (uid2 != NULL, 0);
+
+	return CF_CLASS (folder)->cmp_uids (folder, uid1, uid2);
+}
+
+static gint
+cmp_array_uids (gconstpointer a, gconstpointer b, gpointer user_data)
+{
+	const char *uid1 = *(const char **) a;
+	const char *uid2 = *(const char **) b;
+	CamelFolder *folder = user_data;
+
+	g_return_val_if_fail (CAMEL_IS_FOLDER (folder), 0);
+
+	return camel_folder_cmp_uids (folder, uid1, uid2);
 }
 
 static void
 sort_uids (CamelFolder *folder, GPtrArray *uids)
 {
-	qsort (uids->pdata, uids->len, sizeof (void *), uidcmp);
+	g_qsort_with_data (uids->pdata, uids->len, sizeof (void *), cmp_array_uids, folder);
 }
 
 
Index: camel/camel-folder.h
===================================================================
--- camel/camel-folder.h	(revision 10220)
+++ camel/camel-folder.h	(working copy)
@@ -178,6 +178,7 @@ typedef struct {
 	void (*free_uids)             (CamelFolder *folder,
 				       GPtrArray *array);
 	
+	gint (* cmp_uids) (CamelFolder *folder, const char *uid1, const char *uid2);
 	void (* sort_uids) (CamelFolder *folder, GPtrArray *uids);
 	
 	GPtrArray * (*get_summary)    (CamelFolder *folder);
@@ -320,6 +321,7 @@ void               camel_folder_free_uid
 GPtrArray *        camel_folder_get_uncached_uids     (CamelFolder *,
                                                        GPtrArray * uids,
                                                        CamelException *);
+gint 		   camel_folder_cmp_uids 	      (CamelFolder *folder, const char *uid1, const char *uid2);
 void               camel_folder_sort_uids             (CamelFolder *folder,
 						       GPtrArray *uids);
 
Index: camel/camel-db.c
===================================================================
--- camel/camel-db.c	(revision 10220)
+++ camel/camel-db.c	(working copy)
@@ -901,7 +901,7 @@ read_uids_flags_callback (void *ref, int
 }
 
 int
-camel_db_get_folder_uids_flags (CamelDB *db, char *folder_name, char *sort_by, char *collate, GPtrArray *summary, GHashTable *table, CamelException *ex)
+camel_db_get_folder_uids_flags (CamelDB *db, const char *folder_name, const char *sort_by, const char *collate, GPtrArray *summary, GHashTable *table, CamelException *ex)
 {
 	 GPtrArray *uids = summary;
 	 GPtrArray *flags = g_ptr_array_new ();
@@ -946,7 +946,7 @@ read_uids_callback (void *ref, int ncol,
 }
 
 int
-camel_db_get_folder_uids (CamelDB *db, char *folder_name, char *sort_by, char *collate, GPtrArray *array, CamelException *ex)
+camel_db_get_folder_uids (CamelDB *db, const char *folder_name, const char *sort_by, const char *collate, GPtrArray *array, CamelException *ex)
 {
 	 char *sel_query;
 	 int ret;
Index: camel/camel-db.h
===================================================================
--- camel/camel-db.h	(revision 10220)
+++ camel/camel-db.h	(working copy)
@@ -162,8 +162,8 @@ GPtrArray * camel_db_get_vuids_from_vfol
 int camel_db_add_to_vfolder (CamelDB *db, char *folder_name, char *vuid, CamelException *ex);
 int camel_db_add_to_vfolder_transaction (CamelDB *db, char *folder_name, char *vuid, CamelException *ex);
 
-int camel_db_get_folder_uids (CamelDB *db, char *folder_name, char *sort_by, char *collate, GPtrArray *array, CamelException *ex);
-int camel_db_get_folder_uids_flags (CamelDB *db, char *folder_name, char *sort_by, char *collate, GPtrArray *summary, GHashTable *table, CamelException *ex);
+int camel_db_get_folder_uids (CamelDB *db, const char *folder_name, const char *sort_by, const char *collate, GPtrArray *array, CamelException *ex);
+int camel_db_get_folder_uids_flags (CamelDB *db, const char *folder_name, const char *sort_by, const char *collate, GPtrArray *summary, GHashTable *table, CamelException *ex);
 
 GPtrArray * camel_db_get_folder_junk_uids (CamelDB *db, char *folder_name, CamelException *ex);
 GPtrArray * camel_db_get_folder_deleted_uids (CamelDB *db, char *folder_name, CamelException *ex);
Index: camel/ChangeLog
===================================================================
--- camel/ChangeLog	(revision 10220)
+++ camel/ChangeLog	(working copy)
@@ -1,3 +1,22 @@
+2009-04-22  Milan Crha  <mcrha@redhat.com>
+
+	** Part of fix for bug #563954
+
+	* camel-folder.h: (camel_folder_cmp_uids), (struct CamelFolder):
+	* camel-folder.c: (cmp_uids), (camel_folder_class_init),
+	(camel_folder_cmp_uids), (cmp_array_uids), (sort_uids):
+	Define camel_folder_cmp_uids to compare UIDs properly.
+	* camel-folder-summary.h: (camel_folder_summary_ensure_infos_loaded):
+	* camel-folder-summary.c: (camel_folder_summary_ensure_infos_loaded):
+	Define new function.
+
+	* camel-db.h: (camel_db_get_folder_uids),
+	(camel_db_get_folder_uids_flags):
+	* camel-db.c: (camel_db_get_folder_uids),
+	(camel_db_get_folder_uids_flags):
+	* camel-folder-summary.c: (camel_folder_summary_load_from_db):
+	Functions prototype cleanup.
+
 2009-04-15  Sergio Villar Senin  <svillar@igalia.com>
 
 	** Fix for bug #578823 – invalid condition
Index: camel/camel-folder-summary.c
===================================================================
--- camel/camel-folder-summary.c	(revision 10220)
+++ camel/camel-folder-summary.c	(working copy)
@@ -924,6 +924,20 @@ camel_folder_summary_reload_from_db (Cam
 	return ret == 0 ? 0 : -1;
 }
 
+/**
+ * camel_folder_summary_ensure_infos_loaded:
+ *
+ * Loads all infos into memory, if they are not yet.
+ **/
+void
+camel_folder_summary_ensure_infos_loaded (CamelFolderSummary *s, CamelException *ex)
+{
+	g_return_if_fail (s != NULL);
+
+	if ((s->uids ? s->uids->len : 0) != g_hash_table_size (s->loaded_infos))
+		camel_folder_summary_reload_from_db (s, ex);
+}
+
 static void 
 camel_folder_summary_dump (CamelFolderSummary *s)
 {
@@ -963,7 +977,7 @@ camel_folder_summary_load_from_db (Camel
 	folder_name = s->folder->full_name;
 	cdb = s->folder->parent_store->cdb_r;
 
-	ret = camel_db_get_folder_uids_flags (cdb, folder_name, (char *)s->sort_by, (char *)s->collate, s->uids, p->flag_cache, ex);
+	ret = camel_db_get_folder_uids_flags (cdb, folder_name, s->sort_by, s->collate, s->uids, p->flag_cache, ex);
 	/* camel_folder_summary_dump (s); */
 
 #if 0
Index: camel/camel-folder-summary.h
===================================================================
--- camel/camel-folder-summary.h	(revision 10220)
+++ camel/camel-folder-summary.h	(working copy)
@@ -365,6 +365,8 @@ GPtrArray * camel_folder_summary_get_cha
 int camel_folder_summary_cache_size (CamelFolderSummary *s);
 /* reload the summary at any required point if required */
 int camel_folder_summary_reload_from_db (CamelFolderSummary *s, CamelException *ex);
+/* ensures all CamelMessagesInfos loaded in memory */
+void camel_folder_summary_ensure_infos_loaded (CamelFolderSummary *s, CamelException *ex);
 /* insert mi to summary */
 void camel_folder_summary_insert (CamelFolderSummary *s, CamelMessageInfo *info, gboolean load);
 
