Sort map keys when serialising map fields to binary and text formats.

This gives deterministic output, albeit somewhat inefficently.
I expect the inefficiency to be dwarfed by the reflection involved
anyway, but it's easy enough to improve this later if needed.
diff --git a/proto/encode.go b/proto/encode.go
index f5050e3..1512d60 100644
--- a/proto/encode.go
+++ b/proto/encode.go
@@ -1101,7 +1101,9 @@
 		return nil
 	}
 
-	for _, key := range v.MapKeys() {
+	keys := v.MapKeys()
+	sort.Sort(mapKeys(keys))
+	for _, key := range keys {
 		val := v.MapIndex(key)
 
 		keycopy.Set(key)
diff --git a/proto/lib.go b/proto/lib.go
index fe0818c..87c6b9d 100644
--- a/proto/lib.go
+++ b/proto/lib.go
@@ -736,3 +736,16 @@
 
 	return dm
 }
+
+// Map fields may have key types of non-float scalars, strings and enums.
+// The easiest way to sort them in some deterministic order is to use fmt.
+// If this turns out to be inefficient we can always consider other options,
+// such as doing a Schwartzian transform.
+
+type mapKeys []reflect.Value
+
+func (s mapKeys) Len() int      { return len(s) }
+func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+func (s mapKeys) Less(i, j int) bool {
+	return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
+}
diff --git a/proto/text.go b/proto/text.go
index f41a946..720eac4 100644
--- a/proto/text.go
+++ b/proto/text.go
@@ -247,6 +247,7 @@
 		if fv.Kind() == reflect.Map {
 			// Map fields are rendered as a repeated struct with key/value fields.
 			keys := fv.MapKeys() // TODO: should we sort these for deterministic output?
+			sort.Sort(mapKeys(keys))
 			for _, key := range keys {
 				val := fv.MapIndex(key)
 				if err := writeName(w, props); err != nil {