Method to sort keys in map.

This commit is contained in:
Andrey Smirnov
2014-01-21 13:36:09 +04:00
parent e183ddb981
commit e21506d373
2 changed files with 18 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package utils
import (
"fmt"
"sort"
)
// StringsIsSubset checks that subset is strict subset of full, and returns
@@ -62,3 +63,15 @@ func StrSliceHasItem(s []string, item string) bool {
}
return false
}
// StrMapSortedKeys returns keys of map[string]string sorted
func StrMapSortedKeys(m map[string]string) []string {
keys := make(sort.StringSlice, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}

View File

@@ -42,3 +42,8 @@ func (s *ListSuite) TestStrSliceHasIteml(c *C) {
c.Check(StrSliceHasItem([]string{"a", "b"}, "b"), Equals, true)
c.Check(StrSliceHasItem([]string{"a", "b"}, "c"), Equals, false)
}
func (s *ListSuite) TestStrMapSortedKeys(c *C) {
c.Check(StrMapSortedKeys(map[string]string{}), DeepEquals, []string{})
c.Check(StrMapSortedKeys(map[string]string{"x": "1", "a": "3", "y": "4"}), DeepEquals, []string{"a", "x", "y"})
}