From db26c97e7bc0dbbb62063549f41e7a45f7284c4b Mon Sep 17 00:00:00 2001 From: coward Date: Fri, 3 Dec 2021 16:16:00 +0800 Subject: [PATCH] =?UTF-8?q?:memo:=20=E6=9B=B4=E6=96=B0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++++++++- maps.go | 7 ------- maps_test.go | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0ab1ba2..7416c99 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,20 @@ maps.Values() // GetMapsOriginMap get this maps origin data return a map[string]interface maps.OriginMap() -// SortKey sort this maps by keys +// SortKey sort this maps by keys asc maps.SortKey() +// other sort +// sort by desc +sort.Slice(maps.Keys,func (i,j int) bool { + return maps.Keys()[i] > maps.Keys()[j] +}) + +// sort by asc +sort.Slice(maps.Keys,func (i,j int) bool { + return maps.Keys()[i] < maps.Keys()[j] +}) + + // other example ./maps_test.go ``` \ No newline at end of file diff --git a/maps.go b/maps.go index 169f0a3..e77beb5 100644 --- a/maps.go +++ b/maps.go @@ -5,13 +5,6 @@ import ( "sync" ) -const ( - // SortAsc sort by asc - SortAsc = "asc" - // SortDesc sort by desc - SortDesc = "desc" -) - type maps struct { rw sync.RWMutex Key []string diff --git a/maps_test.go b/maps_test.go index 1e07534..f87cf0f 100644 --- a/maps_test.go +++ b/maps_test.go @@ -62,3 +62,17 @@ func TestSortKey(t *testing.T) { fmt.Println(a) } + +func TestSort(t *testing.T) { + maps := NewMaps() + + a := maps.Set("key3", "value1").Set("key4", "value 2").Set("key1", "value 3") + + sort.Slice(a.Keys(), func(i, j int) bool { + return a.Keys()[i] > a.Keys()[j] + }) + + fmt.Println(a.Keys()) + fmt.Println(a.Values()) + fmt.Println(a.OriginMap()) +}