Maelstrom: Sort the preferences before saving them, so the prefs file is easy to scan for debugging purposes.

https://github.com/libsdl-org/Maelstrom/commit/ba912e1270139e6934afa4ebf95a61623347488e

From ba912e1270139e6934afa4ebf95a61623347488e Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 7 Nov 2011 21:01:38 -0500
Subject: [PATCH] Sort the preferences before saving them, so the prefs file is
 easy to scan for debugging purposes.

---
 utils/prefs.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/utils/prefs.cpp b/utils/prefs.cpp
index 984342a3..b40a7a5c 100644
--- a/utils/prefs.cpp
+++ b/utils/prefs.cpp
@@ -18,6 +18,7 @@
 
 #include "SDL.h"
 #include "physfs.h"
+#include "array.h"
 #include "hashtable.h"
 
 #include "prefs.h"
@@ -30,6 +31,12 @@ hash_nuke_strings(const void *key, const void *value, void *data)
 	SDL_free((char*)value);
 }
 
+static int
+sort_keys(const void *a, const void *b)
+{
+	return SDL_strcasecmp(*(const char **)a, *(const char **)b);
+}
+
 Prefs::Prefs(const char *file)
 {
 	m_file = SDL_strdup(file);
@@ -123,9 +130,16 @@ Prefs::Save()
 		return false;
 	}
 
+	array<const char *> keys;
 	iter = NULL;
 	while (hash_iter(m_values, (const void **)&key, (const void **)&value, &iter)) {
-		if (!writeString(fp, key) ||
+		keys.add(key);
+	}
+	qsort(&keys[0], keys.length(), sizeof(key), sort_keys);
+	
+	for (int i = 0; i < keys.length(); ++i) {
+		hash_find(m_values, keys[i], (const void **)&value);
+		if (!writeString(fp, keys[i]) ||
 		    !writeString(fp, "=") ||
 		    !writeString(fp, value) ||
 		    !writeString(fp, "\r\n")) {