当前位置:网站首页>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好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- [Verilog tutorial]
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
- [reading notes] phased summary of writing reading notes
- Convolution和Batch normalization的融合
- MFC 获取当前时间
- MySQL基础
- Leetcode DP three step problem
- Chapter 4 of getting started with MySQL: data types stored in data tables
- [shutter] open the third-party shutter project
- Maybe you read a fake Tianlong eight
猜你喜欢

Mapper agent development

Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track

JDBC tutorial
![洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表](/img/4a/ab732c41ea8a939fa0983fec475622.png)
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表

Matlab 信号处理【问答笔记-1】
![MATLAB signal processing [Q & a notes-1]](/img/53/ae081820fe81ce28e1f04914678a6f.png)
MATLAB signal processing [Q & a notes-1]

监控容器运行时工具Falco

直击产业落地!飞桨重磅推出业界首个模型选型工具
![[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)

Pytorch里面多任务Loss是加起来还是分别backward?
随机推荐
Fusion de la conversion et de la normalisation des lots
Analyze ad654: Marketing Analytics
How much do you know about synchronized?
附加:token;(没写完,别看…)
教育学大佬是怎么找外文参考文献的?
67 page overall planning and construction plan for a new smart city (download attached)
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
直击产业落地!飞桨重磅推出业界首个模型选型工具
开发知识点
实用系列丨免费可商用视频素材库
数据集-故障诊断:西储大学轴承的各项数据以及数据说明
[shutter] shutter open source project reference
JSON data transfer parameters
35页危化品安全管理平台解决方案2022版
Leetcode DP three step problem
Leetcode skimming - game 280
监控容器运行时工具Falco
Explain in detail the process of realizing Chinese text classification by CNN
RTP 接发ps流工具改进(二)
MFC file operation