当前位置:网站首页>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好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- 数据集-故障诊断:西储大学轴承的各项数据以及数据说明
- JDBC practice cases
- Xcode real machine debugging
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- 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
- Digital collection trading website domestic digital collection trading platform
- Digital twin smart factory develops digital twin factory solutions
- Optimization of streaming media technology
- Codeforces Round #771 (Div. 2)---A-D
- Returns the size of the largest binary search subtree in a binary tree
猜你喜欢

How much do you know about synchronized?

JS interviewer wants to know how much you understand call, apply, bind no regrets series
![MATLAB signal processing [Q & a notes-1]](/img/53/ae081820fe81ce28e1f04914678a6f.png)
MATLAB signal processing [Q & a notes-1]

35 pages dangerous chemicals safety management platform solution 2022 Edition

附加:token;(没写完,别看…)

Wechat applet basic learning (wxss)

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

RTP 接发ps流工具改进(二)

Intranet penetration | teach you how to conduct intranet penetration hand in hand
![[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)](/img/75/3f6203410dd2754e578e0baaaa9aef.png)
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
随机推荐
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Improvement of RTP receiving and sending PS stream tool (II)
sysdig分析容器系统调用
cocospods 的使用
监控容器运行时工具Falco
经济学外文文献在哪查?
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
95页智慧教育解决方案2022
MFC 获取当前时间
Returns the size of the largest binary search subtree in a binary tree
Convolution和Batch normalization的融合
Speech recognition Series 1: speech recognition overview
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
Container runtime analysis
程序分析与优化 - 9 附录 XLA的缓冲区指派
Hit the industry directly! The propeller launched the industry's first model selection tool
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
RTP 接发ps流工具改进(二)
Bean load control
MySQL advanced learning notes (4)