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:
@@ -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
|
||||
|
Reference in New Issue
Block a user