forked from xx19941215/light-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 845 Bytes
/
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
package main
import (
"sort"
"fmt"
"math/rand"
)
type Hero struct {
Name string
Age int
}
//声明一个 Hero 结构体切片类型
type HeroSlice []Hero
func main() {
var intSlice = []int{0, -1, 7, 10, 90}
sort.Ints(intSlice)
fmt.Println(intSlice)
var heroes HeroSlice
for i := 0; i < 10; i++ {
hero := Hero{
Name: fmt.Sprintf("英雄|%d", rand.Intn(10)),
Age: rand.Intn(10),
}
heroes = append(heroes, hero)
}
for _, v := range(heroes) {
fmt.Println(v)
}
sort.Sort(heroes)
fmt.Println("排序后")
for _, v := range(heroes) {
fmt.Println(v)
}
}
type Student struct {
Name string
Age int
Score float64
}
func (hs HeroSlice) Len() int {
return len(hs)
}
func (hs HeroSlice) Less(i, j int) bool {
return hs[i].Age < hs[j].Age
}
func (hs HeroSlice) Swap(i, j int) {
hs[i], hs[j] = hs[j], hs[i]
}