From 3e8c3bd396be43799e8fd947ba9fca8cfde55a40 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 23 Aug 2024 11:21:29 +0800 Subject: [PATCH] feat: add SortbyKeys for map --- docs/api/packages/maputil.md | 38 +++++++++++++++++++++++++++++++ docs/en/api/packages/maputil.md | 37 ++++++++++++++++++++++++++++++ maputil/map.go | 20 +++++++++++++++++ maputil/map_example_test.go | 16 +++++++++++++ maputil/map_test.go | 40 +++++++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+) diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index c8321bc..3c7d740 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -56,6 +56,7 @@ import ( - [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) +- [SortByKeys](#SortByKeys)
@@ -1522,4 +1523,41 @@ func main() { // a // b } +``` + +### SortByKeys + +

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

+ +函数签名: + +```go +func SortByKeys[K constraints.Ordered, V any](m map[K]V) (sortedKeysMap map[K]V) +``` + +示例:[运行]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result := maputil.SortByKeys(m) + + fmt.Println(result) + + // Output: + // map[1:a 2:b 3:c 4:d] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index d3a3c52..1a98ff2 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -1538,4 +1538,41 @@ func main() { // a // b } +``` + +### SortByKeys + +

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) +``` + +Example:[运行]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result := maputil.SortByKeys(m) + + fmt.Println(result) + + // Output: + // map[1:a 2:b 3:c 4:d] +} ``` \ No newline at end of file diff --git a/maputil/map.go b/maputil/map.go index d0d9d58..f30a178 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -453,3 +453,23 @@ 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. +// Play: todo +func SortByKeys[K constraints.Ordered, V any](m map[K]V) (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] + }) + + sortedKeysMap = make(map[K]V, len(m)) + for _, k := range keys { + sortedKeysMap[k] = m[k] + } + + return +} diff --git a/maputil/map_example_test.go b/maputil/map_example_test.go index e385367..38ae8a7 100644 --- a/maputil/map_example_test.go +++ b/maputil/map_example_test.go @@ -540,3 +540,19 @@ func ExampleGetOrSet() { // a // b } + +func ExampleSortByKeys() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result := SortByKeys(m) + + fmt.Println(result) + + // Output: + // map[1:a 2:b 3:c 4:d] +} diff --git a/maputil/map_test.go b/maputil/map_test.go index 94b8a92..a3208e9 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -707,3 +707,43 @@ func TestGetOrSet(t *testing.T) { assert.Equal("a", result1) assert.Equal("b", result2) } + +func TestSortByKeys(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestSortByKeys") + + m1 := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + expected1 := map[int]string{ + 1: "a", + 2: "b", + 3: "c", + 4: "d", + } + + result1 := SortByKeys(m1) + + assert.Equal(expected1, result1) + + m2 := map[string]int{ + "c": 3, + "a": 1, + "d": 4, + "b": 2, + } + expected2 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + "d": 4, + } + + result2 := SortByKeys(m2) + + assert.Equal(expected2, result2) +}