Index: src/loadsave.c
===================================================================
--- src/loadsave.c	(revision 3712)
+++ src/loadsave.c	(working copy)
@@ -97,7 +97,7 @@
 static void displayLoadSlot		(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset, PIELIGHT *pColours);
 static void displayLoadSaveEdit	(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset, PIELIGHT *pColours);
 void		removeWildcards		(char *pStr);
-
+static int modcmp ( const void *a,  const void *b);
 static	W_SCREEN	*psRequestScreen;					// Widget screen for requester
 static	BOOL		mode;
 static	UDWORD		chosenSlotId;
@@ -110,7 +110,25 @@
 
 static char			sPath[255];
 static char			sExt[4];
+//=========
+struct sorted_list {
+	PHYSFS_sint64 modtime;		//number of seconds since the epoch
+	char fname[totalslotspace];	//actual file name
+	};
+struct sorted_list sortedlist[totalslots];
+//=========
+static int modcmp ( const void *a,  const void *b)
+{
+const  struct sorted_list *val1 = a;
+const  struct sorted_list *val2 = b;
 
+// we want list sorted so that 1st entry is latest file date.
+if (val1->modtime > val2->modtime) return -1;
+else if (val1->modtime < val2->modtime) return 1;
+else return 0;
+
+}
+
 // ////////////////////////////////////////////////////////////////////////////
 // return whether the save screen was displayed in the mission results screen
 BOOL saveInMissionRes(void)
@@ -163,6 +181,8 @@
 	static char	sSlots[totalslots][totalslotspace];
 	char **i, **files;
 	const char* checkExtension;
+	int j = 0,k = 0;
+	BOOL skipsort = FALSE;
 
 	mode = bLoad;
 	debug(LOG_WZ, "_addLoadSave(%d, %s, %s, %s)", bLoad, sSearchPath, sExtension, title);
@@ -299,31 +319,47 @@
 	// Check for an extension like ".ext", not "ext"
 	sasprintf((char**)&checkExtension, ".%s", sExtension);
 
-	// add savegame filenames minus extensions to buttons (up to max 10)
+	memset(sortedlist,0x0,sizeof(sortedlist));	//clear the list
+	skipsort = FALSE;
+	// add savegame filenames minus extensions to buttons (up to max totalslots)
 	files = PHYSFS_enumerateFiles(sSearchPath);
-	for (i = files; *i != NULL; ++i)
+	for (i = files,k=0; (*i != NULL) && (k < totalslots ); ++i)
 	{
+		char s_file[PATH_MAX];
+
+		if (!strstr(*i, checkExtension)) continue;
+		// Construct the full path to the file by appending the
+		// filename to the directory it is in.
+		snprintf(s_file, sizeof(s_file), "%s%s", sPath, *i);
+		// get the modification time.  They say a -1 happens on some sort of error.
+		// if that is the case, we should skip the sort?
+		sortedlist[k].modtime = PHYSFS_getLastModTime(s_file);
+		if (sortedlist[k].modtime == -1) skipsort = TRUE;
+		(*i)[strlen(*i) - 4] = '\0'; // remove .gam extension;
+		sprintf(sortedlist[k].fname,"%s",*i);
+		k++;
+	}
+	// sort the list based on latest = first (change in modcmp)
+	// only if we don't get a error from physfs.
+	if (!skipsort)
+	qsort(&sortedlist,k,sizeof(struct sorted_list),modcmp);
+
+	for ( j = 0; j<k ; j++)
+	{
 		W_BUTTON *button;
 
-		// See if this filename contains the extension we're looking for
-		if (!strstr(*i, checkExtension))
-		{
-			// If it doesn't, move on to the next filename
-			continue;
-		}
-
 		button = (W_BUTTON*)widgGetFromID(psRequestScreen, LOADENTRY_START + slotCount);
-
+		// skip if entry is 0.  Should never happen anyway.
+		if (sortedlist[j].modtime == 0) continue;
 		debug(LOG_WZ, "_addLoadSave: We found [%s]", *i);
 		/* Set the tip and add the button */
-		(*i)[strlen(*i) - 4] = '\0'; // remove .gam extension
-		strlcpy(sSlots[slotCount], *i, sizeof(sSlots[slotCount]));  //store it!
+		strlcpy(sSlots[slotCount], sortedlist[j].fname, sizeof(sSlots[slotCount]));  //store it!
 		button->pTip = sSlots[slotCount];
 		button->pText = sSlots[slotCount];
 		slotCount++;		// goto next but...
 		if (slotCount == totalslots)
 		{
-			break;	// only show up to 10 entries
+			break;	// only show up to totalslots entries
 		}
 	}
 	PHYSFS_freeList(files);
