mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-09 06:04:12 +00:00
Method to sort keys in map.
This commit is contained in:
@@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StringsIsSubset checks that subset is strict subset of full, and returns
|
// StringsIsSubset checks that subset is strict subset of full, and returns
|
||||||
@@ -62,3 +63,15 @@ func StrSliceHasItem(s []string, item string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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"}, "b"), Equals, true)
|
||||||
c.Check(StrSliceHasItem([]string{"a", "b"}, "c"), Equals, false)
|
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"})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user