]> go.fuhry.dev Git - runtime.git/commitdiff
utils/hashset: add AsSortedSlice()
authorDan Fuhry <dan@fuhry.com>
Tue, 27 Feb 2024 20:56:27 +0000 (15:56 -0500)
committerDan Fuhry <dan@fuhry.com>
Tue, 27 Feb 2024 20:56:27 +0000 (15:56 -0500)
utils/hashset/hashset.go

index 7509728fd1b6533c425c916749c6517b57e42b1f..40cfd8bf4c5e48e90b61fefa2a3d5ad465be1b8d 100644 (file)
@@ -1,6 +1,8 @@
 package hashset
 
 import (
+       "fmt"
+       "sort"
        "sync"
 )
 
@@ -118,6 +120,19 @@ func (hs *HashSet[TKey]) AsSlice() []TKey {
        return hs.asSlice()
 }
 
+func (hs *HashSet[TKey]) AsSortedSlice() []TKey {
+       hs.lock.RLock()
+       defer hs.lock.RUnlock()
+
+       sl := hs.asSlice()
+
+       sort.Slice(sl, func(i, j int) bool {
+               return fmt.Sprintf("%+v", sl[i]) < fmt.Sprintf("%+v", sl[j])
+       })
+
+       return sl
+}
+
 func (hs *HashSet[TKey]) asSlice() []TKey {
        s := make([]TKey, len(hs.data))