diff --git a/filter/map.go b/filter/map.go index 5a56ce1..e56e572 100644 --- a/filter/map.go +++ b/filter/map.go @@ -3,7 +3,10 @@ package filter -import "fmt" +import ( + "fmt" + "sort" +) // Println prints ever element of the set. func Println[T P](set []T) { @@ -11,3 +14,13 @@ func Println[T P](set []T) { fmt.Println(i) } } + +// Keys returns the keys in lexicographically sorted order. +func Keys[T any](m map[string]T) []string { + keys := []string{} + for k, _ := range m { + keys = append(keys, k) + sort.Strings(keys) + } + return keys +} diff --git a/filter/map_test.go b/filter/map_test.go index 6eb086c..cdbdf10 100644 --- a/filter/map_test.go +++ b/filter/map_test.go @@ -3,7 +3,11 @@ package filter_test -import "github.com/rwxrob/bonzai/filter" +import ( + "fmt" + + "github.com/rwxrob/bonzai/filter" +) func ExamplePrintln() { set := []string{"doe", "ray", "mi"} @@ -18,3 +22,13 @@ func ExamplePrintln() { // true // true } + +func ExampleKeys() { + m1 := map[string]int{"two": 2, "three": 3, "one": 1} + m2 := map[string]string{"two": "two", "three": "three", "one": "one"} + fmt.Println(filter.Keys(m1)) + fmt.Println(filter.Keys(m2)) + // Output: + // [one three two] + // [one three two] +}