Clean up sortedKeys function

This introduces a generic function SortedStringKeys which can be used to
get a slice of sorted string keys for all kinds of maps having string
keys.

Bug: N/A
Test: m
Change-Id: I542194c68984d909b7ad1dbf060d4d3a98f0ef23
This commit is contained in:
Inseob Kim
2019-06-08 15:47:51 +09:00
parent 4cb61bed13
commit 1a365c6a7f
4 changed files with 15 additions and 29 deletions

View File

@@ -16,6 +16,7 @@ package android
import (
"fmt"
"reflect"
"regexp"
"runtime"
"sort"
@@ -77,10 +78,15 @@ func JoinWithSuffix(strs []string, suffix string, separator string) string {
return string(ret)
}
func sortedKeys(m map[string][]string) []string {
s := make([]string, 0, len(m))
for k := range m {
s = append(s, k)
func SortedStringKeys(m interface{}) []string {
v := reflect.ValueOf(m)
if v.Kind() != reflect.Map {
panic(fmt.Sprintf("%#v is not a map", m))
}
keys := v.MapKeys()
s := make([]string, 0, len(keys))
for _, key := range keys {
s = append(s, key.String())
}
sort.Strings(s)
return s