-
Notifications
You must be signed in to change notification settings - Fork 22
/
sortDescriptor.go
216 lines (174 loc) · 6.46 KB
/
sortDescriptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package fpgo
import (
"reflect"
"strings"
)
// NewComparableOrdered Generate a Ordered Comparable for Comparator
func NewComparableOrdered[T Ordered](val T) ComparableOrdered[T] {
return ComparableOrdered[T]{
Val: val,
}
}
// ComparableOrdered A Ordered Comparable for Comparator
type ComparableOrdered[T Ordered] struct {
Val T
}
// CompareTo Compare with an another object
func (obj ComparableOrdered[T]) CompareTo(input interface{}) int {
return CompareToOrdered(obj.Val, input.(ComparableOrdered[T]).Val)
}
// NewComparableString Generate a String Comparable for Comparator
func NewComparableString(val string) ComparableString {
return ComparableString{
Val: val,
}
}
// ComparableString A String Comparable for Comparator
type ComparableString struct {
Val string
}
// CompareTo Compare with an another object
func (obj ComparableString) CompareTo(input interface{}) int {
return strings.Compare(string(obj.Val), string(input.(ComparableString).Val))
}
// SortDescriptor Define a Transformer Pattern SortDescriptor
type SortDescriptor[T any] interface {
Transformer[T, Comparable[any]]
IsAscending() bool
SetAscending(bool)
}
// SortedListBySortDescriptors Sort items by sortDescriptors and return value
func SortedListBySortDescriptors[T any](sortDescriptors []SortDescriptor[T], input ...T) []T {
result := append(input[:0:0], input...)
SortBySortDescriptors(sortDescriptors, result)
return result
}
// SortBySortDescriptors Sort items by sortDescriptors
func SortBySortDescriptors[T any](sortDescriptors []SortDescriptor[T], input []T) {
Sort(func(item1 T, item2 T) bool {
return _compareBySortDescriptors(item1, item2, sortDescriptors, 0) >= 0
}, input)
}
func _compareBySortDescriptors[T any](item1 T, item2 T, sortDescriptors []SortDescriptor[T], descriptorIndex int) int {
descriptor := sortDescriptors[descriptorIndex]
key1 := descriptor.TransformedBy()(item1)
key2 := descriptor.TransformedBy()(item2)
result := 0
if key1 != nil && key2 != nil {
if descriptor.IsAscending() {
key1.CompareTo(key2)
} else {
key2.CompareTo(key1)
}
}
if key1 != nil && key2 == nil {
if descriptor.IsAscending() {
return 1
}
return -1
}
if key1 == nil && key2 != nil {
if descriptor.IsAscending() {
return -1
}
return 1
}
if result == 0 && _hasNextDescriptor(sortDescriptors, descriptorIndex) {
return _compareBySortDescriptors(item1, item2, sortDescriptors, descriptorIndex+1)
}
return result
}
func _hasNextDescriptor[T any](sortDescriptors []SortDescriptor[T], index int) bool {
return (index + 1) < len(sortDescriptors)
}
// SimpleSortDescriptor
// NewSimpleSortDescriptor Generate a new SimpleSortDescriptor by TransformerFunctor & ascending(true)/descending(false)
func NewSimpleSortDescriptor[T any](transformFn TransformerFunctor[T, Comparable[interface{}]], ascending bool) SimpleSortDescriptor[T] {
return SimpleSortDescriptor[T]{
transformFn: transformFn,
ascending: ascending,
}
}
// SimpleSortDescriptor SimpleSortDescriptor implemented by TransformerFunctor
type SimpleSortDescriptor[T any] struct {
ascending bool
transformFn TransformerFunctor[T, Comparable[interface{}]]
}
// IsAscending Check is this SortDescriptor sorting by ascending
func (descriptor SimpleSortDescriptor[T]) IsAscending() bool {
return descriptor.ascending
}
// SetAscending Set this SortDescriptor sorting by ascending(true) or descending(false)
func (descriptor SimpleSortDescriptor[T]) SetAscending(val bool) {
descriptor.ascending = val
}
// TransformedBy Get the TransformerFunctor of this SortDescriptor
func (descriptor SimpleSortDescriptor[T]) TransformedBy() TransformerFunctor[T, Comparable[interface{}]] {
return descriptor.transformFn
}
// FieldSortDescriptor
// NewFieldSortDescriptor Generate a new FieldSortDescriptor by FieldName & ascending(true)/descending(false)
func NewFieldSortDescriptor[T any](fieldName string, ascending bool) FieldSortDescriptor[T] {
return FieldSortDescriptor[T]{
SimpleSortDescriptor: SimpleSortDescriptor[T]{
ascending: ascending,
},
fieldName: fieldName,
}
}
// FieldSortDescriptor FieldSortDescriptor implemented by Reflection(by FieldName)
type FieldSortDescriptor[T any] struct {
SimpleSortDescriptor[T]
fieldName string
}
// GetFieldName Get the fieldName to sort
func (descriptor FieldSortDescriptor[T]) GetFieldName() string {
return descriptor.fieldName
}
// SetFieldName Set the fieldName to sort
func (descriptor FieldSortDescriptor[T]) SetFieldName(val string) {
descriptor.fieldName = val
}
// TransformedBy Get the TransformerFunctor of this SortDescriptor
func (descriptor FieldSortDescriptor[T]) TransformedBy() TransformerFunctor[T, Comparable[interface{}]] {
return func(input T) Comparable[interface{}] {
r := reflect.ValueOf(input)
f := reflect.Indirect(r).FieldByName(descriptor.fieldName).Interface()
return f.(Comparable[interface{}])
}
}
// SortDescriptorsBuilder
// NewSortDescriptorsBuilder Generate a new SortDescriptorsBuilder
func NewSortDescriptorsBuilder[T any]() SortDescriptorsBuilder[T] {
return SortDescriptorsBuilder[T]{}
}
// SortDescriptorsBuilder SortDescriptorsBuilder for composing SortDescriptor list and sorting data
type SortDescriptorsBuilder[T any] []SortDescriptor[T]
// ThenWithTransformerFunctor Use TransformerFunctor as a SortDescriptor
func (builder SortDescriptorsBuilder[T]) ThenWithTransformerFunctor(transformFn TransformerFunctor[T, Comparable[interface{}]], ascending bool) SortDescriptorsBuilder[T] {
result := append(builder, NewSimpleSortDescriptor(transformFn, ascending))
return result
}
// ThenWithFieldName Use FieldName as a SortDescriptor
func (builder SortDescriptorsBuilder[T]) ThenWithFieldName(fieldName string, ascending bool) SortDescriptorsBuilder[T] {
result := append(builder, NewFieldSortDescriptor[T](fieldName, ascending))
return result
}
// ThenWith Append a SortDescriptor
func (builder SortDescriptorsBuilder[T]) ThenWith(input ...SortDescriptor[T]) SortDescriptorsBuilder[T] {
result := append(builder, input...)
return result
}
// GetSortDescriptors Get sortDescriptors
func (builder SortDescriptorsBuilder[T]) GetSortDescriptors() []SortDescriptor[T] {
return []SortDescriptor[T](builder)
}
// ToSortedList Get the sorted result
func (builder SortDescriptorsBuilder[T]) ToSortedList(input ...T) []T {
result := SortedListBySortDescriptors(builder.GetSortDescriptors(), input...)
return result
}
// Sort Sort by sortDescriptors
func (builder SortDescriptorsBuilder[T]) Sort(input []T) {
SortBySortDescriptors(builder.GetSortDescriptors(), input)
}