@@ -27,21 +27,36 @@ type orderByValue[V any] struct {
27
27
}
28
28
29
29
type SortFilter [V any , OrderKey comparable , FilterObj comparable ] struct {
30
- orderBys map [OrderKey ]orderByValue [V ]
31
- filters []Filter [V , FilterObj ]
32
- defaultSortKey OrderKey
30
+ orderBys map [OrderKey ]orderByValue [V ]
31
+ filters []Filter [V , FilterObj ]
32
+ tieBreakSortKey OrderKey
33
+ tieBreakOrderDirection model.OrderDirection
33
34
}
34
35
35
- // New creates a new SortFilter with the given defaultSortKey .
36
- // The defaultSortKey is used when two values are equal in the OrderBy function.
37
- // The defaultSortKey must not be registered as a ConcurrentOrderBy.
38
- func New [V any , OrderKey comparable , FilterObj comparable ](defaultSortKey OrderKey ) * SortFilter [V , OrderKey , FilterObj ] {
36
+ // New creates a new SortFilter with the given tieBreakSortKey and tieBreakOrderDirection .
37
+ // The tieBreakSortKey is used when two values are equal in the OrderBy function.
38
+ // The tieBreakSortKey must not be registered as a ConcurrentOrderBy.
39
+ func New [V any , OrderKey comparable , FilterObj comparable ](tieBreakSortKey OrderKey , tieBreakOrderDirection model. OrderDirection ) * SortFilter [V , OrderKey , FilterObj ] {
39
40
return & SortFilter [V , OrderKey , FilterObj ]{
40
- orderBys : make (map [OrderKey ]orderByValue [V ]),
41
- defaultSortKey : defaultSortKey ,
41
+ orderBys : make (map [OrderKey ]orderByValue [V ]),
42
+ tieBreakSortKey : tieBreakSortKey ,
43
+ tieBreakOrderDirection : tieBreakOrderDirection ,
42
44
}
43
45
}
44
46
47
+ func (s * SortFilter [T , OrderKey , FilterObj ]) DefaultSortKey () OrderKey {
48
+ return s .tieBreakSortKey
49
+ }
50
+
51
+ func (s * SortFilter [T , OrderKey , FilterObj ]) DefaultOrderDirection () model.OrderDirection {
52
+ return s .tieBreakOrderDirection
53
+ }
54
+
55
+ func (s * SortFilter [T , OrderKey , FilterObj ]) Supports (key OrderKey ) bool {
56
+ _ , exists := s .orderBys [key ]
57
+ return exists
58
+ }
59
+
45
60
func (s * SortFilter [T , OrderKey , FilterObj ]) RegisterFilter (filter Filter [T , FilterObj ]) {
46
61
s .filters = append (s .filters , filter )
47
62
}
@@ -55,11 +70,6 @@ func (s *SortFilter[T, OrderKey, FilterObj]) RegisterOrderBy(key OrderKey, order
55
70
}
56
71
}
57
72
58
- func (s * SortFilter [T , OrderKey , FilterObj ]) Supports (key OrderKey ) bool {
59
- _ , exists := s .orderBys [key ]
60
- return exists
61
- }
62
-
63
73
func (s * SortFilter [T , OrderKey , FilterObj ]) RegisterConcurrentOrderBy (key OrderKey , orderBy ConcurrentOrderBy [T ]) {
64
74
if _ , ok := s .orderBys [key ]; ok {
65
75
panic (fmt .Sprintf ("OrderBy already registered for key: %v" , key ))
@@ -181,5 +191,9 @@ func (s *SortFilter[T, OrderKey, FilterObj]) sort(ctx context.Context, items []T
181
191
}
182
192
183
193
func (s * SortFilter [T , OrderKey , FilterObj ]) defaultSort (ctx context.Context , a , b T ) int {
184
- return s .orderBys [s .defaultSortKey ].orderBy (ctx , a , b )
194
+ if s .tieBreakOrderDirection == model .OrderDirectionDesc {
195
+ return s .orderBys [s .tieBreakSortKey ].orderBy (ctx , b , a )
196
+ }
197
+
198
+ return s .orderBys [s .tieBreakSortKey ].orderBy (ctx , a , b )
185
199
}
0 commit comments