-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
66 lines (52 loc) · 1.47 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
// Based on the example on https://pkg.go.dev/sort
package main
import (
"fmt"
"sort"
"strings"
)
type City struct {
Name string
Population int
}
func (c City) Clone() City {
return c
}
// Wrap the underlying model to sort.
type sortWrapper []City
// Implements the sort.Interface to allow sorting of custom types.
func (sw sortWrapper) Len() int {
return len(sw)
}
func (sw sortWrapper) Less(i, j int) bool {
return strings.Compare(sw[i].Name, sw[j].Name) < 0
}
func (sw sortWrapper) Swap(i, j int) {
sw[i], sw[j] = sw[j], sw[i]
}
func main() {
cities := []City{
City{Name: "Beijing", Population: 20035455},
City{Name: "Paris", Population: 11078546},
City{Name: "Bangalore", Population: 13193035},
City{Name: "Los Angeles", Population: 3909360},
City{Name: "San Paulo", Population: 21846507},
City{Name: "Nairobi", Population: 5118844},
}
var cities1, cities2 []City
for _, city := range cities {
cities1 = append(cities1, city.Clone())
cities2 = append(cities2, city.Clone())
}
fmt.Println("unsorted cities1:", cities1)
fmt.Println("unsorted cities2:", cities2)
// We can sort by implement wrap the underlying value with methods that define
// the pertinent methods to help with the sort.
sort.Sort(sortWrapper(cities1))
fmt.Println("sorted cities1:", cities1)
// Another method is to just sort it with a closure.
sort.Slice(cities2, func(i, j int) bool {
return cities2[i].Name < cities[j].Name
})
fmt.Println("sorted cities2:", cities2)
}