当前位置:网站首页>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好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- leetcode 650. 2 keys keyboard with only two keys (medium)
- 数据集-故障诊断:西储大学轴承的各项数据以及数据说明
- JDBC教程
- 一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
- Wechat applet basic learning (wxss)
- Digital twin visualization solution digital twin visualization 3D platform
- 容器运行时分析
- Integration of revolution and batch normalization
- Top Devops tool chain inventory
- How to maintain the brand influence of clothing enterprises
猜你喜欢

Integration of revolution and batch normalization

MySQL基础

直击产业落地!飞桨重磅推出业界首个模型选型工具

Difference between NVIDIA n card and amda card

Practical series - free commercial video material library
![[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)

How to set automatic reply for mailbox and enterprise mailbox?

67 page overall planning and construction plan for a new smart city (download attached)

Interpretation of new plug-ins | how to enhance authentication capability with forward auth

Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
随机推荐
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
1380. Lucky numbers in the matrix
Flexible combination of applications is a false proposition that has existed for 40 years
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
【ML】李宏毅三:梯度下降&分类(高斯分布)
Unique line of "Gelu"
leetcode 650. 2 keys keyboard with only two keys (medium)
[Verilog tutorial]
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
请求与响应
JVM foundation review
Agnosticism and practice makes perfect
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
Request and response
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
顶级 DevOps 工具链大盘点
Analyze ad654: Marketing Analytics
Mapper代理开发
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth