forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort.go
62 lines (51 loc) · 1.27 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
package imagequalify
import (
"sort"
"github.com/openshift/origin/pkg/image/admission/imagequalify/api"
)
type lessFunc func(x, y *api.ImageQualifyRule) bool
type multiSorter struct {
rules []api.ImageQualifyRule
less []lessFunc
}
var _ sort.Interface = &multiSorter{}
// Sort sorts the argument slice according to the comparator functions
// passed to orderBy.
func (s *multiSorter) Sort(rules []api.ImageQualifyRule) {
s.rules = rules
sort.Sort(s)
}
// orderBy returns a Sorter that sorts using a number of comparator
// functions.
func orderBy(less ...lessFunc) *multiSorter {
return &multiSorter{
less: less,
}
}
// Len is part of sort.Interface.
func (s *multiSorter) Len() int {
return len(s.rules)
}
// Swap is part of sort.Interface.
func (s *multiSorter) Swap(i, j int) {
s.rules[i], s.rules[j] = s.rules[j], s.rules[i]
}
// Less is part of sort.Interface.
func (s *multiSorter) Less(i, j int) bool {
p, q := s.rules[i], s.rules[j]
// Try all but the last comparison.
var k int
for k = 0; k < len(s.less)-1; k++ {
less := s.less[k]
switch {
case less(&p, &q):
// p < q, so we have a decision.
return true
case less(&q, &p):
// p > q, so we have a decision.
return false
}
// p == q; try the next comparison.
}
return s.less[k](&p, &q)
}