当前位置:网站首页>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
好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- Returns the maximum distance between two nodes of a binary tree
- QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
- 经济学外文文献在哪查?
- Bean加载控制
- 一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
- MySQL基础
- yolov5detect. Py comment
- leetcode 650. 2 keys keyboard with only two keys (medium)
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- Top Devops tool chain inventory
猜你喜欢
How QT exports data to PDF files (qpdfwriter User Guide)
Many to one, one to many processing
Convolution和Batch normalization的融合
maya渔屋建模
sysdig分析容器系统调用
JDBC Exercise case
Hit the industry directly! The propeller launched the industry's first model selection tool
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
[shutter] shutter open source project reference
JDBC practice cases
随机推荐
Mapper代理开发
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
MySQL advanced learning notes (4)
Chapter 4 of getting started with MySQL: data types stored in data tables
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
[shutter] shutter open source project reference
Top Devops tool chain inventory
JDBC練習案例
Define MySQL function to realize multi module call
Use of cocospods
顶级 DevOps 工具链大盘点
Returns the size of the largest binary search subtree in a binary tree
开发知识点
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
哪些软件可以整篇翻译英文论文?
67 page overall planning and construction plan for a new smart city (download attached)
JDBC教程
直击产业落地!飞桨重磅推出业界首个模型选型工具
Installing redis under Linux
返回二叉树中最大的二叉搜索子树的根节点