-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_slice方法.go
52 lines (48 loc) · 979 Bytes
/
1_slice方法.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
/*
* @Author: your name
* @Date: 2020-12-09 09:42:04
* @LastEditTime: 2020-12-09 10:51:45
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /demo/1_slice方法.go
*/
package main
import (
"fmt"
"sort"
)
type student struct {
Name string
Age int
}
type st []student
func (a st) Len() int {
return len(a)
}
func (a st) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a st) Less(i, j int) bool {
return a[i].Age < a[j].Age
}
func Initg() {
sortList := []int{8, 5, 4, 7, 9, 5, 4, 3, 6}
// 复制
// copyList := make([]int,10,10)
copyList := []int{9: 10}
fmt.Printf("co:%T\n", copyList)
copy(copyList, sortList)
fmt.Printf("copy:%v\n", copyList)
// 排序
sort.Ints(sortList)
fmt.Printf("int类型排序:%v\n", sortList)
// 查询是否有某个值
people := []student{
{"tian", 22},
{"he", 46},
{"qiang", 18},
{"jie", 56},
}
sort.Sort(st(people))
fmt.Printf("结构体切片排序:%v\n", people)
}