1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

refactoring: rename SortByKeys to SortByKey

This commit is contained in:
dudaodong
2024-08-28 10:58:14 +08:00
parent a6d39a3bba
commit ca40b5d6c6
5 changed files with 25 additions and 19 deletions

View File

@@ -56,7 +56,7 @@ import (
- [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Has](#ConcurrentMap_Has)
- [ConcurrentMap_Range](#ConcurrentMap_Range) - [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet) - [GetOrSet](#GetOrSet)
- [SortByKeys](#SortByKeys) - [SortByKey](#SortByKey)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1525,14 +1525,14 @@ func main() {
} }
``` ```
### <span id="SortByKeys">SortByKeys</span> ### <span id="SortByKey">SortByKey</span>
<p>对传入的map根据key进行排序返回排序后的map。</p> <p>对传入的map根据key进行排序返回排序后的map。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```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)
``` ```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b> <b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
@@ -1553,7 +1553,7 @@ func main() {
2: "b", 2: "b",
} }
result := maputil.SortByKeys(m) result := maputil.SortByKey(m)
fmt.Println(result) fmt.Println(result)

View File

@@ -1540,14 +1540,14 @@ func main() {
} }
``` ```
### <span id="SortByKeys">SortByKeys</span> ### <span id="SortByKey">SortByKey</span>
<p>Sorts the map by its keys and returns a new map with sorted keys.</p> <p>Sorts the map by its keys and returns a new map with sorted keys.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```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)
``` ```
<b>Example:<span style="float:right;display:inline-block;">[运行]()</span></b> <b>Example:<span style="float:right;display:inline-block;">[运行]()</span></b>
@@ -1568,7 +1568,7 @@ func main() {
2: "b", 2: "b",
} }
result := maputil.SortByKeys(m) result := maputil.SortByKey(m)
fmt.Println(result) fmt.Println(result)

View File

@@ -454,16 +454,16 @@ func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V {
return value 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 // 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)) keys := make([]K, 0, len(m))
for k := range m { for k := range m {
keys = append(keys, k) keys = append(keys, k)
} }
sort.Slice(keys, func(i, j int) bool { 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)) sortedKeysMap = make(map[K]V, len(m))

View File

@@ -541,7 +541,7 @@ func ExampleGetOrSet() {
// b // b
} }
func ExampleSortByKeys() { func ExampleSortByKey() {
m := map[int]string{ m := map[int]string{
3: "c", 3: "c",
1: "a", 1: "a",
@@ -549,7 +549,9 @@ func ExampleSortByKeys() {
2: "b", 2: "b",
} }
result := SortByKeys(m) result := SortByKey(m, func(a, b int) bool {
return a < b
})
fmt.Println(result) fmt.Println(result)

View File

@@ -708,10 +708,10 @@ func TestGetOrSet(t *testing.T) {
assert.Equal("b", result2) assert.Equal("b", result2)
} }
func TestSortByKeys(t *testing.T) { func TestSortByKey(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestSortByKeys") assert := internal.NewAssert(t, "TestSortByKey")
m1 := map[int]string{ m1 := map[int]string{
3: "c", 3: "c",
@@ -726,7 +726,9 @@ func TestSortByKeys(t *testing.T) {
4: "d", 4: "d",
} }
result1 := SortByKeys(m1) result1 := SortByKey(m1, func(a, b int) bool {
return a < b
})
assert.Equal(expected1, result1) assert.Equal(expected1, result1)
@@ -737,13 +739,15 @@ func TestSortByKeys(t *testing.T) {
"b": 2, "b": 2,
} }
expected2 := map[string]int{ expected2 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4, "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) assert.Equal(expected2, result2)
} }