深入解读go语言之 排序: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, 0, 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, a, b int)
// 快速排序
func quickSort(data Interface, a, b, maxDepth int)
// 归并排序
func symMerge(data Interface, a, m, b int)
  • 再看个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 (
    "sort"
    "fmt"
)
type person struct {
    Name string
    Age int
}

type personSlice []person
// 这三个方法必须有,相当于实现了sort.Interface
func (s personSlice) Len() int { return len(s) }
func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age }  // 这里是关键,我比较了年龄这个字段

func main() {
    a := personSlice {
        {
            Name: "AAA",
            Age: 55,
        },
        {
            Name: "BBB",
            Age: 22,
        },
        {
            Name: "CCC",
            Age: 0,
        },
        {
            Name: "DDD",
            Age: 22,
        },
        {
            Name: "EEE",
            Age: 11,
        },
    }
    sort.Sort(a)
    fmt.Println(a)
}

列2(倒序):



package main

import (
	"fmt"
	"sort"
)
type person struct {
	Name int
	Age int
}

type personSlice  []person
// 这三个方法必须有,相当于实现了sort.Interface
func (s personSlice) Len() int { return len(s) }
func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i]}
func (s personSlice) Less(i, j int) bool {return s[i].Age < s[j].Age}// 这里是关键,我比较了年龄这个字段 //升序

func main() {
	a := personSlice {
		{
			Name: 2,
			Age: 55,
		},
		{
			Name: 1,
			Age: 22,
		},
		{
			Name: 3,
			Age: 0,
		},
		{
			Name: 4,
			Age: 22,
		},
		{
			Name: 5,
			Age: 11,
		},
	}

	var or =make(map[int]int)
   //先将将a结构体类型的切片遍历给 or map中
	for b, b2 :=range a{
		fmt.Println(b)
		or[b2.Name] =b2.Age
		fmt.Println(or[b2.Name],"***",b2.Name,"or[b2.name]--------------------------------->>>",b2.Age)
	}
	fmt.Println("or----------",or)
	fmt.Println("len(a)-------",len(a))
	//初始化排序的slice,长度就是a的长度
	ps :=make(personSlice,len(a))
	i:=0
	//遍历 map  将值遍历给  person结构体中
	//以新声明的变量i为索引值为person 对应的切片为personSlice
	for k,v :=range or{
		ps[i] =person{k,v}
		i++
	}
	fmt.Println("aaaaaaaaaaaaaaaaaaa",a)
	fmt.Println("ps---------",ps)
	//sort.Sort(ps)
	//对ps切片进行排序
	sort.Sort(a)
	fmt.Println("排序后的a",a)
	sort.Sort(sort.Reverse(ps))
	fmt.Println(ps)
}



深入解读go语言之 排序:sort

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值