-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsort.go
55 lines (43 loc) · 1 KB
/
sort.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
package ogdl
import (
"sort"
"strings"
)
type stringSorter struct {
fields string
array []*Graph
}
func (g *Graph) Sort(field string) {
s := stringSorter{field, g.Out}
sort.Sort(s)
}
func (g *Graph) SortAsString(field string) {
s := stringSorter{field, g.Out}
sort.Sort(s)
}
func (g stringSorter) Swap(i, j int) { g.array[i], g.array[j] = g.array[j], g.array[i] }
func (g stringSorter) Less(i, j int) bool {
a := g.array[i].Get(g.fields).String()
b := g.array[j].Get(g.fields).String()
return strings.Compare(a, b) == -1
}
func (g stringSorter) Len() int {
return len(g.array)
}
type intSorter struct {
fields string
array []*Graph
}
func (g *Graph) SortAsInt(field string) {
s := intSorter{field, g.Out}
sort.Sort(s)
}
func (g intSorter) Swap(i, j int) { g.array[i], g.array[j] = g.array[j], g.array[i] }
func (g intSorter) Less(i, j int) bool {
a := g.array[i].Get(g.fields).Int64()
b := g.array[j].Get(g.fields).Int64()
return a < b
}
func (g intSorter) Len() int {
return len(g.array)
}