-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
65 lines (56 loc) · 1.5 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
package contacts
type ByName []*Contact
func (b ByName) Len() int { return len(b) }
func (b ByName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByName) Less(i, j int) bool { return compareDisplay(b[i], b[j]) }
type ByLastName []*Contact
func (b ByLastName) Len() int { return len(b) }
func (b ByLastName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByLastName) Less(i, j int) bool { return compareName(b[i], b[j]) }
type ByBirthday []*Contact
func (b ByBirthday) Len() int { return len(b) }
func (b ByBirthday) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByBirthday) Less(i, j int) bool { return compareBirthday(b[i], b[j]) }
func compareBirthday(lhs, rhs *Contact) bool {
lb, rb := lhs.birthdayOrZero(), rhs.birthdayOrZero()
if lb.Month() == rb.Month() {
if lb.Day() == rb.Day() {
if lb.Year() == rb.Year() {
return compareDisplay(lhs, rhs)
}
return lb.Year() < rb.Year()
}
return lb.Day() < rb.Day()
}
return lb.Month() < rb.Month()
}
func compareDisplay(lhs, rhs *Contact) bool {
if lhs == rhs {
return false
}
if lhs == nil {
return true
}
if rhs == nil {
return false
}
return lhs.DisplayName() < rhs.DisplayName()
}
func compareName(lhs, rhs *Contact) bool {
if lhs == rhs {
return false
}
if lhs == nil {
return true
}
if rhs == nil {
return false
}
if lhs.Last == rhs.Last {
if lhs.First == rhs.First {
return compareDisplay(lhs, rhs)
}
return lhs.First < rhs.First
}
return lhs.Last < rhs.Last
}