-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (69 loc) · 1.55 KB
/
main.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
package main
import (
"fmt"
"sort"
)
type user struct {
First string
Last string
Age int
Sayings []string
}
type byIdade []user
func (u byIdade) Len() int { return len(u) }
func (u byIdade) Less(i, j int) bool { return u[i].Age < u[j].Age }
func (u byIdade) Swap(i, j int) { u[i], u[j] = u[j], u[i]}
type byLast []user
func (u byLast) Len() int { return len(u) }
func (u byLast) Less(i, j int) bool { return u[i].Last < u[j].Last }
func (u byLast) Swap(i, j int) { u[i], u[j] = u[j], u[i]}
func main() {
u2 := user{
First: "James",
Last: "Bond",
Age: 32,
Sayings: []string{
"Shaken, not stirred",
"Youth is no guarantee of innovation",
"In his majesty's royal service",
},
}
u3 := user{
First: "Miss",
Last: "Moneypenny",
Age: 27,
Sayings: []string{
"James, it is soo good to see you",
"Would you like me to take care of that for you, James?",
"I would really prefer to be a secret agent myself.",
},
}
u1 := user{
First: "M",
Last: "Hmmmm",
Age: 54,
Sayings: []string{
"Oh, James. You didn't.",
"Dear God, what has James done now?",
"Can someone please tell me where James Bond is?",
},
}
users := []user{u1, u2, u3}
fmt.Println("original:")
legal(users)
sort.Sort(byIdade(users))
fmt.Println("idade:")
legal(users)
sort.Sort(byLast(users))
fmt.Println("Last name:")
legal(users)
}
func legal(u []user) {
for _, us := range u {
fmt.Println(" ",us.First,us.Last)
fmt.Println(" ",us.Age,"anos")
for _, v := range us.Sayings {
fmt.Println("\t->",v)
}
}
}