当前位置:网站首页>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 .
边栏推荐
- MFC文件操作
- QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
- TypeError: Cannot read properties of undefined (reading ***)
- 开发知识点
- sourcetree 详细
- Use of cocospods
- [shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
- 哪些软件可以整篇翻译英文论文?
- MFC gets the current time
- sysdig分析容器系统调用
猜你喜欢
JSON data transfer parameters
Architecture: database architecture design
Sysdig analysis container system call
Hit the industry directly! The propeller launched the industry's first model selection tool
容器运行时分析
Optimization of streaming media technology
MATLAB signal processing [Q & a notes-1]
Interface difference test - diffy tool
流媒体技术优化
Additional: token; (don't read until you finish writing...)
随机推荐
Is there a specific format for English papers?
论文的英文文献在哪找(除了知网)?
Returns the size of the largest binary search subtree in a binary tree
Pytorch里面多任务Loss是加起来还是分别backward?
35 pages dangerous chemicals safety management platform solution 2022 Edition
Improvement of RTP receiving and sending PS stream tool (II)
Codeforces Round #771 (Div. 2)---A-D
实用系列丨免费可商用视频素材库
Using tensorflow to realize voiceprint recognition
请问大家在什么网站上能查到英文文献?
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
MFC file operation
秒杀系统设计
Which software can translate an English paper in its entirety?
JDBC practice cases
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
How much do you know about synchronized?
sourcetree 详细
How much do you know about synchronized?
Leetcode relaxation question - day of the week