golang 对自定义类型排序
golang 对自定义类型排序
由于 golang 的 sort 包本身就提供了相应的功能, 我们就没必要重复的造个轮子了,来看看如何利用 sort 包来实现吧。 sort包浅谈
粗略的看看sort包func Sort(data Interface) {
// Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
n := data.Len()
maxDepth := 0
for i := n; i > 0; i >>= 1 {
maxDepth++
}
maxDepth *= 2
quickSort(data,n,maxDepth)
}
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i,j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i,j int)
}
// 内部实现的四种排序算法
// 插入排序
func insertionSort(data Interface,a,b int)
// 堆排序
func heapSort(data Interface,b int)
// 快速排序
func quickSort(data Interface,b,maxDepth int)
// 归并排序
func symMerge(data Interface,m,b int)
所以要调用 sort.Sort() 来实现自定义类型排序,只需要我们的类型实现 Interface 接口类型中的三个方法即可。 先看看 sort 包本身对于 []int 类型如何排序 // 首先定义了一个[]int类型的别名IntSlice
type IntSlice []int
// 获取此 slice 的长度
func (p IntSlice) Len() int { return len(p) }
// 比较两个元素大小 升序
func (p IntSlice) Less(i,j int) bool { return p[i] < p[j] }
// 交换数据
func (p IntSlice) Swap(i,j int) { p[i],p[j] = p[j],p[i] }
// sort.Ints()内部调用Sort() 方法实现排序
// 注意 要先将[]int 转换为 IntSlice类型 因为此类型才实现了Interface的三个方法
func Ints(a []int) { Sort(IntSlice(a)) }
照葫芦画瓢 我们来对自定义的结构体类型进行降序排序 package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
type Persons []Person
// 获取此 slice 的长度
func (p Persons) Len() int { return len(p) }
// 根据元素的年龄降序排序 (此处按照自己的业务逻辑写)
func (p Persons) Less(i,j int) bool {
return p[i].Age > p[j].Age
}
// 交换数据
func (p Persons) Swap(i,j int) { p[i],p[i] }
func main() {
persons := Persons{
{
Name: "test1",Age: 20,},{
Name: "test2",Age: 22,{
Name: "test3",Age: 21,}
fmt.Println("排序前")
for _,person := range persons {
fmt.Println(person.Name,":",person.Age)
}
sort.Sort(persons)
fmt.Println("排序后")
for _,person.Age)
}
}
其实,一般 Len() 和 Swap() 基本不做改变,只有涉及到元素比较的 Less() 方法会有所改变。 package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
type Persons []Person
// Len()方法和Swap()方法不用变化
// 获取此 slice 的长度
func (p Persons) Len() int { return len(p) }
// 交换数据
func (p Persons) Swap(i,p[i] }
// 嵌套结构体 将继承 Person 的所有属性和方法
// 所以相当于SortByName 也实现了 Len() 和 Swap() 方法
type SortByName struct{ Persons }
// 根据元素的姓名长度降序排序 (此处按照自己的业务逻辑写)
func (p SortByName) Less(i,j int) bool {
return len(p.Persons[i].Name) > len(p.Persons[j].Name)
}
type SortByAge struct{ Persons }
// 根据元素的年龄降序排序 (此处按照自己的业务逻辑写)
func (p SortByAge) Less(i,j int) bool {
return p.Persons[i].Age > p.Persons[j].Age
}
func main() {
persons := Persons{
{
Name: "test123",{
Name: "test1",{
Name: "test12",person.Age)
}
sort.Sort(SortByName{persons})
fmt.Println("排序后")
for _,person.Age)
}
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
