-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting-struct.go
57 lines (48 loc) · 1.3 KB
/
sorting-struct.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
package main
import (
"fmt"
"sort"
)
type person struct {
Name string
Age int
}
type byAge []person
func (s byAge) Len() int {
return len(s)
}
func (s byAge) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byAge) Less(i, j int) bool {
return s[i].Age < s[j].Age
}
// Go’s sort package implements sorting for builtins and user-defined types.
func main() {
matt := person{"Matt", 33}
john := person{"John", 42}
jane := person{"Jane", 29}
persons := []person{matt, jane, john}
fmt.Printf("Persons pointer: %p\n", &persons)
usingFunction(&persons)
fmt.Println()
persons = []person{john, jane, matt}
fmt.Printf("Persons pointer: %p\n", &persons)
usingPointerReceiver(&persons)
}
func usingFunction(persons *[]person) {
fmt.Printf("Persons pointer in usingFunction: %p\n", persons)
fmt.Println("Persons:", *persons)
// Use 'persons[:]' for an array and 'persons' for a slice
// sort.Slice(*persons[:], func(i, j int) bool {
sort.Slice(*persons, func(i, j int) bool {
return (*persons)[i].Age < (*persons)[j].Age
})
fmt.Println("Persons sorted by age:", *persons)
}
func usingPointerReceiver(persons *[]person) {
fmt.Printf("Persons pointer in usingPointerReceiver: %p\n", &persons)
fmt.Println("Persons:", *persons)
sort.Sort(byAge(*persons))
fmt.Println("Persons sorted by age:", *persons)
}