Repository navigation
Expand file tree
/
Copy pathsortDescriptor.go
More file actions
181 lines (145 loc) · 5.31 KB
/
Copy pathsortDescriptor.go
File metadata and controls
181 lines (145 loc) · 5.31 KB
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
package fpgo
import (
"reflect"
)
// SortDescriptor Define a Transformer Pattern SortDescriptor
type SortDescriptor interface {
Transformer
IsAscending() bool
SetAscending(bool)
}
// SortedListBySortDescriptors Sort items by sortDescriptors and return value
func SortedListBySortDescriptors(sortDescriptors []SortDescriptor, input ...interface{}) []interface{} {
result := append(input[:0:0], input...)
SortBySortDescriptors(sortDescriptors, result)
return result
}
// SortBySortDescriptors Sort items by sortDescriptors
func SortBySortDescriptors(sortDescriptors []SortDescriptor, input []interface{}) {
Sort(func(item1 interface{}, item2 interface{}) bool {
return _compareBySortDescriptors(item1, item2, sortDescriptors, 0) >= 0
}, input)
}
func _compareBySortDescriptors(item1 interface{}, item2 interface{}, sortDescriptors []SortDescriptor, descriptorIndex int) int {
descriptor := sortDescriptors[descriptorIndex]
key1 := descriptor.TransformedBy()(item1).(Comparable)
key2 := descriptor.TransformedBy()(item2).(Comparable)
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(sortDescriptors []SortDescriptor, index int) bool {
return (index + 1) < len(sortDescriptors)
}
// SimpleSortDescriptor
// NewSimpleSortDescriptor Generate a new SimpleSortDescriptor by TransformerFunctor & ascending(true)/descending(false)
func NewSimpleSortDescriptor(transformFn TransformerFunctor, ascending bool) SimpleSortDescriptor {
return SimpleSortDescriptor{
transformFn: transformFn,
ascending: ascending,
}
}
// SimpleSortDescriptor SimpleSortDescriptor implemented by TransformerFunctor
type SimpleSortDescriptor struct {
ascending bool
transformFn TransformerFunctor
}
// IsAscending Check is this SortDescriptor sorting by ascending
func (descriptor SimpleSortDescriptor) IsAscending() bool {
return descriptor.ascending
}
// SetAscending Set this SortDescriptor sorting by ascending(true) or descending(false)
func (descriptor SimpleSortDescriptor) SetAscending(val bool) {
descriptor.ascending = val
}
// TransformedBy Get the TransformerFunctor of this SortDescriptor
func (descriptor SimpleSortDescriptor) TransformedBy() TransformerFunctor {
return descriptor.transformFn
}
// FieldSortDescriptor
// NewFieldSortDescriptor Generate a new FieldSortDescriptor by FieldName & ascending(true)/descending(false)
func NewFieldSortDescriptor(fieldName string, ascending bool) FieldSortDescriptor {
return FieldSortDescriptor{
SimpleSortDescriptor: SimpleSortDescriptor{
ascending: ascending,
},
fieldName: fieldName,
}
}
// FieldSortDescriptor FieldSortDescriptor implemented by Reflection(by FieldName)
type FieldSortDescriptor struct {
SimpleSortDescriptor
fieldName string
}
// GetFieldName Get the fieldName to sort
func (descriptor FieldSortDescriptor) GetFieldName() string {
return descriptor.fieldName
}
// SetFieldName Set the fieldName to sort
func (descriptor FieldSortDescriptor) SetFieldName(val string) {
descriptor.fieldName = val
}
// TransformedBy Get the TransformerFunctor of this SortDescriptor
func (descriptor FieldSortDescriptor) TransformedBy() TransformerFunctor {
return func(input interface{}) interface{} {
r := reflect.ValueOf(input)
f := reflect.Indirect(r).FieldByName(descriptor.fieldName).Interface()
return f
}
}
// SortDescriptorsBuilder
// NewSortDescriptorsBuilder Generate a new SortDescriptorsBuilder
func NewSortDescriptorsBuilder() SortDescriptorsBuilder {
return SortDescriptorsBuilder{}
}
// SortDescriptorsBuilder SortDescriptorsBuilder for composing SortDescriptor list and sorting data
type SortDescriptorsBuilder []SortDescriptor
// ThenWithTransformerFunctor Use TransformerFunctor as a SortDescriptor
func (builder SortDescriptorsBuilder) ThenWithTransformerFunctor(transformFn TransformerFunctor, ascending bool) SortDescriptorsBuilder {
result := append(builder, NewSimpleSortDescriptor(transformFn, ascending))
return result
}
// ThenWithFieldName Use FieldName as a SortDescriptor
func (builder SortDescriptorsBuilder) ThenWithFieldName(fieldName string, ascending bool) SortDescriptorsBuilder {
result := append(builder, NewFieldSortDescriptor(fieldName, ascending))
return result
}
// ThenWith Append a SortDescriptor
func (builder SortDescriptorsBuilder) ThenWith(input ...SortDescriptor) SortDescriptorsBuilder {
result := append(builder, input...)
return result
}
// GetSortDescriptors Get sortDescriptors
func (builder SortDescriptorsBuilder) GetSortDescriptors() []SortDescriptor {
return []SortDescriptor(builder)
}
// ToSortedList Get the sorted result
func (builder SortDescriptorsBuilder) ToSortedList(input ...interface{}) []interface{} {
result := SortedListBySortDescriptors(builder.GetSortDescriptors(), input...)
return result
}
// Sort Sort by sortDescriptors
func (builder SortDescriptorsBuilder) Sort(input []interface{}) {
SortBySortDescriptors(builder.GetSortDescriptors(), input)
}