-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort.go
85 lines (72 loc) · 1.58 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
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
package main
import (
"go/ast"
"go/token"
"sort"
)
type SortableSpec struct {
s ast.Spec
pos token.Pos
}
type SortableSpecs []*SortableSpec
type SortableDecl struct {
d ast.Decl
pos token.Pos
}
type SortableDecls []*SortableDecl
// Len is the number of elements in the collection.
func (s SortableSpecs) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s SortableSpecs) Less(i, j int) bool {
return s[i].pos < s[j].pos
}
// Swap swaps the elements with indexes i and j.
func (s SortableSpecs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Len is the number of elements in the collection.
func (s SortableDecls) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s SortableDecls) Less(i, j int) bool {
return s[i].pos < s[j].pos
}
// Swap swaps the elements with indexes i and j.
func (s SortableDecls) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func sortSpecs(in map[token.Pos]ast.Spec) []ast.Spec {
sIn := make(SortableSpecs, 0, len(in))
for p, s := range in {
sIn = append(sIn, &SortableSpec{
s: s,
pos: p,
})
}
sort.Sort(sIn)
ret := make([]ast.Spec, len(sIn))
for i, s := range sIn {
ret[i] = s.s
}
return ret
}
func sortDecls(in map[token.Pos]ast.Decl) []ast.Decl {
sIn := make(SortableDecls, 0, len(in))
for p, d := range in {
sIn = append(sIn, &SortableDecl{
d: d,
pos: p,
})
}
sort.Sort(sIn)
ret := make([]ast.Decl, len(sIn))
for i, s := range sIn {
ret[i] = s.d
}
return ret
}