diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index 3c7d740..75347d3 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -56,7 +56,7 @@ import ( - [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) -- [SortByKeys](#SortByKeys) +- [SortByKey](#SortByKey)
@@ -1525,14 +1525,14 @@ func main() { } ``` -### SortByKeys +### SortByKey

对传入的map根据key进行排序,返回排序后的map。

函数签名: ```go -func SortByKeys[K constraints.Ordered, V any](m map[K]V) (sortedKeysMap map[K]V) +func SortByKey[K constraints.Ordered, V any](m map[K]V) (sortedKeysMap map[K]V) ``` 示例:[运行]() @@ -1553,7 +1553,7 @@ func main() { 2: "b", } - result := maputil.SortByKeys(m) + result := maputil.SortByKey(m) fmt.Println(result) diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index 1a98ff2..1801875 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -1540,14 +1540,14 @@ func main() { } ``` -### SortByKeys +### SortByKey

Sorts the map by its keys and returns a new map with sorted keys.

Signature: ```go -func SortByKeys[K constraints.Ordered, V any](m map[K]V) (sortedKeysMap map[K]V) +func SortByKey[K constraints.Ordered, V any](m map[K]V) (sortedKeysMap map[K]V) ``` Example:[运行]() @@ -1568,7 +1568,7 @@ func main() { 2: "b", } - result := maputil.SortByKeys(m) + result := maputil.SortByKey(m) fmt.Println(result) diff --git a/maputil/map.go b/maputil/map.go index f30a178..e60222e 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -454,16 +454,16 @@ func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V { return value } -// SortByKeys sorts the map by its keys and returns a new map with sorted keys. +// SortByKey sorts the map by its keys and returns a new map with sorted keys. // Play: todo -func SortByKeys[K constraints.Ordered, V any](m map[K]V) (sortedKeysMap map[K]V) { +func SortByKey[K constraints.Ordered, V any](m map[K]V, less func(a, b K) bool) (sortedKeysMap map[K]V) { keys := make([]K, 0, len(m)) for k := range m { keys = append(keys, k) } sort.Slice(keys, func(i, j int) bool { - return keys[i] < keys[j] + return less(keys[i], keys[j]) }) sortedKeysMap = make(map[K]V, len(m)) diff --git a/maputil/map_example_test.go b/maputil/map_example_test.go index 38ae8a7..d7a88da 100644 --- a/maputil/map_example_test.go +++ b/maputil/map_example_test.go @@ -541,7 +541,7 @@ func ExampleGetOrSet() { // b } -func ExampleSortByKeys() { +func ExampleSortByKey() { m := map[int]string{ 3: "c", 1: "a", @@ -549,7 +549,9 @@ func ExampleSortByKeys() { 2: "b", } - result := SortByKeys(m) + result := SortByKey(m, func(a, b int) bool { + return a < b + }) fmt.Println(result) diff --git a/maputil/map_test.go b/maputil/map_test.go index a3208e9..90b33bb 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -708,10 +708,10 @@ func TestGetOrSet(t *testing.T) { assert.Equal("b", result2) } -func TestSortByKeys(t *testing.T) { +func TestSortByKey(t *testing.T) { t.Parallel() - assert := internal.NewAssert(t, "TestSortByKeys") + assert := internal.NewAssert(t, "TestSortByKey") m1 := map[int]string{ 3: "c", @@ -726,7 +726,9 @@ func TestSortByKeys(t *testing.T) { 4: "d", } - result1 := SortByKeys(m1) + result1 := SortByKey(m1, func(a, b int) bool { + return a < b + }) assert.Equal(expected1, result1) @@ -737,13 +739,15 @@ func TestSortByKeys(t *testing.T) { "b": 2, } expected2 := map[string]int{ - "a": 1, - "b": 2, - "c": 3, "d": 4, + "c": 3, + "b": 2, + "a": 1, } - result2 := SortByKeys(m2) + result2 := SortByKey(m2, func(a, b string) bool { + return a > b + }) assert.Equal(expected2, result2) }