当前位置:网站首页>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好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- 英文论文有具体的格式吗?
- MySQL advanced learning notes (4)
- 返回二叉树中最大的二叉搜索子树的根节点
- 洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
- Leetcode relaxation question - day of the week
- collections. What is the purpose of chainmap- What is the purpose of collections. ChainMap?
- 【OJ】两个数组的交集(set、哈希映射 ...)
- Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
- Codeforces Round #771 (Div. 2)---A-D
- 秒杀系统设计
猜你喜欢

Maybe you read a fake Tianlong eight

实用系列丨免费可商用视频素材库

Optimization of streaming media technology

CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)

数据集-故障诊断:西储大学轴承的各项数据以及数据说明

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

CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)

Use redis to realize self increment serial number

Master the development of facial expression recognition based on deep learning (based on paddlepaddle)

What are the projects of metauniverse and what are the companies of metauniverse
随机推荐
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
Load balancing cluster (LBC)
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
Sourcetree details
容器运行时分析
Digital twin visualization solution digital twin visualization 3D platform
yolov5train. py
Several methods of the minimum value in the maximum value of group query
95 pages of smart education solutions 2022
[shutter] open the third-party shutter project
论文的英文文献在哪找(除了知网)?
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
AcWing_188. 武士风度的牛_bfs
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
JSON data transfer parameters
监控容器运行时工具Falco