当前位置:网站首页>Go custom sort
Go custom sort
2022-07-03 00:18:00 【JunesFour】
Go Custom sort
List of articles
C++ Implement custom sorting in , Can be in sort On the last parameter bit of the function , Add a Customized ratio function
perhaps functor
To achieve :
lambda expression
// v yes vector<int> type
sort(v.begin(), v.end(), [](int a, int b) {
return a > b;
});
functor
class SortInt {
public:
bool operator() (int a, int b) {
return a > b;
}
};
sort(v.begin(), v.end(), SortInt());
Go Through... In language sort Interfaces and functions provided by the package , You can also customize sorting .
Three basic types of ascending sort
If the slice to be sorted is int64, float64, string
type , And it is sorted in ascending order , You can use the following three functions to sort :
sort.Ints(x []int)
sort.Float64s(x []float64)
sort.Strings(x []string)
sort.Sort(data Interface)
This function can sort slices of custom types , The premise is that this custom type must be implemented Interface
Interface .
Interface
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
int Examples of types
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)) // Reverse order
}
struct Examples of types
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)
This function can sort the slice types , You also need to provide a return value of bool Function object of type .
int Examples of types
arr := []int{
1, 4, 6, 3, 10}
sort.Slice(arr, func(i, j int) bool {
return arr[i] > arr[j]
})
struct Examples of types
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)
It seems to use sort.Slice
It seems more convenient , Later, we found more advanced functions and added .
边栏推荐
- 有哪些比较推荐的论文翻译软件?
- Sourcetree details
- 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
- Flexible combination of applications is a false proposition that has existed for 40 years
- 130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
- Which websites can I search for references when writing a thesis?
- 带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
- Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
- Practical series - free commercial video material library
- Digital twin smart factory develops digital twin factory solutions
猜你喜欢
How much do you know about synchronized?
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
Which software can translate an English paper in its entirety?
MATLAB signal processing [Q & a notes-1]
How QT exports data to PDF files (qpdfwriter User Guide)
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Maybe you read a fake Tianlong eight
接口差异测试——Diffy工具
程序分析与优化 - 9 附录 XLA的缓冲区指派
Seckill system design
随机推荐
Interface automation coverage statistics - used by Jacobo
How much do you know about synchronized?
95 pages of smart education solutions 2022
英文论文有具体的格式吗?
sysdig分析容器系统调用
AcWing_188. 武士风度的牛_bfs
MySQL advanced learning notes (4)
Where can I find the English literature of the thesis (except HowNet)?
JDBC practice cases
Explain in detail the process of realizing Chinese text classification by CNN
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
Monitor container runtime tool Falco
大学生课堂作业2000~3000字的小论文,标准格式是什么?
cocospods 的使用
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
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
List of major chip Enterprises
Pytorch里面多任务Loss是加起来还是分别backward?
PR FAQ, what about PR preview video card?