当前位置:网站首页>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
好像更加方便一些,之后发现更加高级的功能再追加.
边栏推荐
- Chapter 3 of getting started with MySQL: database creation and operation
- How much do you know about synchronized?
- 顶级 DevOps 工具链大盘点
- Practical series - free commercial video material library
- MFC gets the current time
- JDBC practice cases
- 1380. Lucky numbers in the matrix
- Interface automation coverage statistics - used by Jacobo
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
- Leetcode relaxation question - day of the week
猜你喜欢
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
Is the multitasking loss in pytoch added up or backward separately?
MATLAB signal processing [Q & a notes-1]
Dishes launcher small green program and directory management (efficiency tool)
How much do you know about synchronized?
RTP 接发ps流工具改进(二)
Pytorch里面多任务Loss是加起来还是分别backward?
95 pages of smart education solutions 2022
95页智慧教育解决方案2022
随机推荐
Use of cocospods
Why can't the start method be called repeatedly? But the run method can?
Interface difference test - diffy tool
67页新型智慧城市整体规划建设方案(附下载)
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
Installing redis under Linux
Is the multitasking loss in pytoch added up or backward separately?
论文的英文文献在哪找(除了知网)?
How to maintain the brand influence of clothing enterprises
Intranet penetration | teach you how to conduct intranet penetration hand in hand
95 pages of smart education solutions 2022
Unique line of "Gelu"
[reading notes] phased summary of writing reading notes
Flexible combination of applications is a false proposition that has existed for 40 years
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
MFC 获取当前时间
A single element in an ordered array -- Valentine's Day mental problems
程序分析与优化 - 9 附录 XLA的缓冲区指派
直击产业落地!飞桨重磅推出业界首个模型选型工具
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)