-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting04.go
76 lines (59 loc) · 1.39 KB
/
sorting04.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
/* Alta3 Research | RZFeeser
SOLUTION 01 - Sorting struct ByHeight */
package main
import (
"fmt"
"sort"
)
// record a "Person" Name, Age, Height (cm)
type Person struct {
Name string
Age int
Height int
}
// return a formatted string
func (p Person) String() string {
return fmt.Sprintf("%s: %d %d", p.Name, p.Age, p.Height)
}
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
func (a ByAge) Len() int {
return len(a)
}
func (a ByAge) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByAge) Less(i, j int) bool {
return a[i].Age < a[j].Age
}
// ByHeight implements sort.Interface for []Person based on
// the Height field.
type ByHeight []Person
func (a ByHeight) Len() int {
return len(a)
}
func (a ByHeight) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByHeight) Less(i, j int) bool {
return a[i].Height < a[j].Height
}
func main() {
// Name, Age, Height (cm)
people := []Person{
{"Bob", 31, 163},
{"John", 42, 175},
{"Michael", 17, 190},
{"Jenny", 26, 180},
}
fmt.Println(people)
// We defined the interface for sort.Sort
// a set of methods for the slice type, as with ByAge, and
// call sort.Sort.
sort.Sort(ByAge(people))
fmt.Println(people)
// Sort by Height
sort.Sort(ByHeight(people))
fmt.Println(people)
}