当前位置:网站首页>Go自定义排序
Go自定义排序
2022-07-02 22:58:00 【JunesFour】
Go自定义排序
C++中实现自定义排序,可以在sort函数的最后一个参数位上,加上一个自定义的比大小函数或者仿函数来实现:
lambda表达式
// v是vector<int>类型
sort(v.begin(), v.end(), [](int a, int b) {
return a > b;
});
仿函数
class SortInt {
public:
bool operator() (int a, int b) {
return a > b;
}
};
sort(v.begin(), v.end(), SortInt());
Go语言中通过sort包提供的接口和函数,也可以实现自定义排序.
三种基本类型升序排序
如果要排序的切片是int64, float64, string类型,且是升序排序,可以使用下面三个函数进行排序:
sort.Ints(x []int)
sort.Float64s(x []float64)
sort.Strings(x []string)
sort.Sort(data Interface)
这个函数可以对自定义类型的切片进行排序,前提是这种自定义类型得实现Interface接口.
Interface
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
int类型举例
type sortInt []int
func (arr sortInt) Len() int {
return len(arr)
}
func (arr sortInt) Less(i, j int) bool {
return arr[i] < arr[j]
}
func (arr sortInt) Swap(i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}
func main() {
arr := []int{
1, 4, 6, 3, 10}
var _arr sortInt = arr
sort.Sort(_arr)
sort.Sort(sort.Reverse(_arr)) // 逆序排序
}
struct类型举例
type Students []Student
func (s Students) Len() int {
return len(s)
}
func (s Students) Less(i, j int) bool {
return s[i].score < s[j].score
}
func (s Students) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func main() {
var students Students
students = []Student{
Student{
"zhangsan", 89},
Student{
"lisi", 98},
Student{
"wangwu", 78},
}
sort.Sort(students)
}
sort.Slice(x interface{}, less func(i, j int) bool)
这个函数可以对切片类型进行排序,还需要提供一个返回值为bool类型的函数对象.
int类型举例
arr := []int{
1, 4, 6, 3, 10}
sort.Slice(arr, func(i, j int) bool {
return arr[i] > arr[j]
})
struct类型举例
students := []Student{
Student{
"zhangsan", 89},
Student{
"lisi", 98},
Student{
"wangwu", 78},
}
sort.Slice(students, func(i, j int) bool {
return students[i].score > students[j].score
})
fmt.Println(students)
这样看来使用sort.Slice好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- 流媒体技术优化
- sourcetree 详细
- Judge whether the binary tree is full binary tree
- 開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
- Interface difference test - diffy tool
- 附加:token;(没写完,别看…)
- 1380. Lucky numbers in the matrix
- sysdig分析容器系统调用
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
猜你喜欢

来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能

JSON数据传递参数

Container runtime analysis

基于OpenCV实现口罩识别

How to set automatic reply for mailbox and enterprise mailbox?

带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
![[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)](/img/c5/2f65d37682607aab98443d7f1ba775.jpg)
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

JS interviewer wants to know how much you understand call, apply, bind no regrets series
随机推荐
Maybe you read a fake Tianlong eight
sourcetree 详细
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
95 pages of smart education solutions 2022
Leetcode DP three step problem
Difference between NVIDIA n card and amda card
Leetcode relaxation question - day of the week
Top Devops tool chain inventory
Use of cocospods
Digital twin smart factory develops digital twin factory solutions
英文论文有具体的格式吗?
JSON data transfer parameters
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
返回二叉树两个节点间的最大距离
S12. Verify multi host SSH mutual access script based on key
Realization of mask recognition based on OpenCV
Installing redis under Linux
Explain in detail the process of realizing Chinese text classification by CNN
哪些软件可以整篇翻译英文论文?