编程笔记

lifelong learning & practice makes perfect

go-struct-sort

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
// 用于staff的排序
type StaffWrapper struct {
Staffs []*Staff
Compare func(a, b *Staff) bool
}

func (sw StaffWrapper) Len() int {
return len(sw.Staffs)
}

func (sw StaffWrapper) Swap(a, b int) {
sw.Staffs[a], sw.Staffs[b] = sw.Staffs[b], sw.Staffs[a]
}
func (sw StaffWrapper) Less(i, j int) bool {
return sw.Compare(sw.Staffs[i], sw.Staffs[j])
}

func SortStaff(staffs []*Staff, by SortBy) {
sort.Sort(StaffWrapper{
Staffs: staffs,
Compare: by,
})
}

SortStaff(staffs, func(a, b *Staff) bool {
return a.BelongCorpID < b.BelongCorpID
})

//参考:https://www.cnblogs.com/wangchaowei/p/7802811.html

欢迎关注我的其它发布渠道