From 9c25f361d4883a74534666205f0e0ea2583137e1 Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Fri, 22 Apr 2022 17:32:40 +0800 Subject: [PATCH] sort map --- collections/generics/sort_map.go | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/collections/generics/sort_map.go b/collections/generics/sort_map.go index 0a8ab04..60cf362 100644 --- a/collections/generics/sort_map.go +++ b/collections/generics/sort_map.go @@ -2,7 +2,6 @@ package generics import ( "fmt" - "sort" "strings" "golang.org/x/exp/constraints" @@ -21,6 +20,32 @@ func NewSortMap[K constraints.Ordered, V any](m map[K]V) *mapSorter[K, V] { } } +func (s *mapSorter[K, V]) Set(key K, value V) { + s.m[key] = value + s.keys = append(s.keys, key) +} + +func (s *mapSorter[K, V]) Get(key K) (V, bool) { + v, ok := s.m[key] + return v, ok +} + +func (s *mapSorter[K, V]) Delete(key K) { + delete(s.m, key) + + keys := keys(s.m) + s.keys = keys +} + +func (s *mapSorter[K, V]) Clear() { + s.m = map[K]V{} + s.keys = []K{} +} + +func (s *mapSorter[K, V]) Count() int { + return len(s.m) +} + func (s *mapSorter[K, V]) Asc() *mapSorter[K, V] { keys := s.keys slices.Sort(keys) @@ -34,12 +59,8 @@ func (s *mapSorter[K, V]) Asc() *mapSorter[K, V] { func (s *mapSorter[K, V]) Desc() *mapSorter[K, V] { keys := s.keys - // slices.SortFunc(keys, func(a, b E) bool { - // return a > b - // }) - - sort.Slice(keys, func(i, j int) bool { - return keys[i] > keys[j] + slices.SortFunc(keys, func(a, b K) bool { + return a > b }) return &mapSorter[K, V]{