-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
72 lines (63 loc) · 1.47 KB
/
util.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
package hpcmodel
import (
"fmt"
"sort"
)
type LessFunc func(item1, item2 *Item) bool
func CreatePropertyLessFunc(id string) LessFunc {
return func(item1, item2 *Item) bool {
p1 := item1.GetProperty(id)
p2 := item2.GetProperty(id)
switch prop1 := p1.(type) {
case *StringProperty:
prop2 := p2.(*StringProperty)
return prop1.Data < prop2.Data
case *NumberProperty:
prop2 := p2.(*NumberProperty)
return prop1.Data < prop2.Data
case *DateTimeProperty:
prop2 := p2.(*DateTimeProperty)
return prop1.Data.Before(prop2.Data)
case *TimestampProperty:
prop2 := p2.(*DateTimeProperty)
return prop1.Data.Before(prop2.Data)
case nil:
panic(fmt.Errorf("prop %s not found", id))
default:
panic(fmt.Errorf("prop %s type not supported", id))
}
}
}
type ItemSorter struct {
items []Item
less []LessFunc
}
func (sorter *ItemSorter) Sort(items []Item) {
sorter.items = items
sort.Sort(sorter)
}
func CreateSorter(less ...LessFunc) *ItemSorter {
return &ItemSorter{
less: less,
}
}
func (sorter *ItemSorter) Len() int {
return len(sorter.items)
}
func (sorter *ItemSorter) Swap(i, j int) {
sorter.items[i], sorter.items[j] = sorter.items[j], sorter.items[i]
}
func (sorter *ItemSorter) Less(i, j int) bool {
p, q := &sorter.items[i], &sorter.items[j]
var k int
for k = 0; k < len(sorter.less)-1; k++ {
less := sorter.less[k]
switch {
case less(p, q):
return true
case less(q, p):
return false
}
}
return sorter.less[k](p, q)
}