int切片排序
var is []int = []int{1, 3, 5, 4, 7}
sort.Ints(is)
fmt.Println(is)
结果
-> % go run test1.go
[1 3 4 5 7]
int切片操作
package main
import (
"fmt"
"sort"
)
func main() {
fd := sort.IntSlice{1, 4, 6, 2}
fd = append(fd, 5)
fmt.Println("========长度==========")
fmt.Println(fd.Len()) //长度 结果为 4
fmt.Println("========比较大小==========")
fmt.Println(fd.Less(1, 3)) //比较 第2个是否小于第4个 false
fmt.Println("========排序==========")
fd.Sort() //排序
fmt.Println(fd)
fmt.Println("========交换值的位置==========")
fd.Swap(2, 3) //值的交换 交换第3个 和第4个
fmt.Println(fd)
fmt.Println("========查找值是否存在==========")
fmt.Println(fd.Search(6)) //查找某个元素是否在切片中,有的话返回下标
fmt.Println(fd[2])
fmt.Println("========查找值不存在==========")
fmt.Println(fd.Search(19)) //查找某个元素是否在切片中,不存在返回最大下标+1 也就是长度
fmt.Println("========int切片是否是有序的==========")
fmt.Println(fd) //[1 2 5 4 6]
bo := sort.IntsAreSorted(fd) //false
fmt.Println(bo) //false
var fds []int = []int{1, 2, 4, 7}
bo = sort.IntsAreSorted(fds) //false
fmt.Println(bo) //true
}
}
结果
-> % go run test1.go
========长度==========
5
========比较大小==========
false
========排序==========
[1 2 4 5 6]
========交换值的位置==========
[1 2 5 4 6]
========查找值是否存在==========
4
5
========查找值不存在==========
5
========int切片是否是有序的==========
[1 2 5 4 6]
false
true
float64切片操作
和上边是一样的
string[]操作
package main
import (
"fmt"
"sort"
)
func main() {
fmt.Println("============常规[]string排序============")
var str []string = []string{"a", "f", "b", "l"}
sort.Strings(str)
fmt.Println(str)
fmt.Println("============sort包[]string排序============")
st := sort.StringSlice{"a", "m", "b", "h"}
fmt.Println("============[]string长度============")
fmt.Println(st.Len())
fmt.Println("============[]string交换============")
fmt.Println(st)
st.Swap(1, 3)
fmt.Println(st)
fmt.Println("============[]string比较下标对应的值的大小============")
bo := st.Less(1, 2)
fmt.Println(bo)
fmt.Println("============[]string查找是否存在============")
n := st.Search("m") //存在返回对应下标
fmt.Println(n)
n = st.Search("md") //不存在返回最大下表+1 也就是长度
fmt.Println(n)
fmt.Println("============[]string排序============")
fmt.Println(st)
st.Sort()
fmt.Println(st)
fmt.Println("============[]string是否是有序的============")
sort.StringsAreSorted()
}
结果
-> % go run test1.go
============常规[]string排序============
[a b f l]
============sort包[]string排序============
============[]string长度============
4
============[]string交换============
[a m b h]
[a h b m]
============[]string比较下标对应的值的大小============
false
============[]string查找是否存在============
3
4
============[]string排序============
[a h b m]
[a b h m]
其他也是可以做这些的
只需要实现sort.interface就可以拥有上边的功能
type Interface
type Interface interface {
// Len方法返回集合中的元素个数
Len() int
// Less方法报告索引i的元素是否比索引j的元素小
Less(i, j int) bool
// Swap方法交换索引i和j的两个元素
Swap(i, j int)
}
一个满足sort.Interface接口的(集合)类型可以被本包的函数进行排序。方法要求集合中的元素可以被整数索引。
实现其他类型自动排序
common.adcf
JobSchedulerPlan struct {
Job *Job
Expr *cronexpr.Expression
NextTime time.Time
SchedulerTime int64
}
sort.go
/**
* @Author: zhangsan
* @Description:
* @File: sort
* @Version: 1.0.0
* @Date: 2021/2/4 下午4:58
*/
package xxx
import "xxx.xxx.com/xxxx/xx-xx-xx/xxx/common"
// byTime is a wrapper for sorting the entry array by time
// (with zero time at the end).
type byTime []*common.adcf
func (s byTime) Len() int { return len(s) }
func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byTime) Less(i, j int) bool {
// Two zero times should return false.
// Otherwise, zero is "greater" than any other time.
// (To sort it at the end of the list.)
if s[i].NextTime.IsZero() {
return false
}
if s[j].NextTime.IsZero() {
return true
}
return s[i].NextTime.Before(s[j].NextTime)
}
使用
jobSortSlice []*common.JobSchedulerPlan
sort.Sort(byTime(s.jobSortSlice))