Go语言golang调用sort.Slice实现struct切片的快速排序
sort.Slice声明
func Slice(slice interface{}, less func(i, j int) bool) {
    rv := reflectValueOf(slice)
    swap := reflectSwapper(slice)
    length := rv.Len()
    quickSort_func(lessSwap{less, swap}, 0, length, maxDepth(length))
}实际使用
和C++的sort模板类似,只需要实现less函数,Go特别的是传入的函数不是直接传入less,而是一个匿名函数,匿名函数的参数是两个下标,表示两个比较元素在切片中的下标
type Person struct {
    h int
    k int
}
func PersonLess(p Person, other Person) bool{
    if p.h > other.h {
        return true
    } else if p.h < other.h {
        return false
    } else {
        return p.k <= other.k
    }
}
func reconstructQueue(people [][]int) [][]int {
    personSlice := NewPersonSlice(people)
    sort.Slice(personSlice, func(i, j int) bool {
        return PersonLess(personSlice[i], personSlice[j])
    })
} 相关推荐
  cuixingwudi    2020-07-26  
   数据与算法之美    2020-07-05  
   路漫    2020-06-26  
   earthhouge    2020-06-25  
   yunfeitian    2020-06-13  
   klarclm    2020-06-13  
   zhixingheyitian    2020-05-28  
   qscool    2020-05-17  
   cuiweisaidelike    2020-05-11  
   winmeanyoung    2020-05-08  
   alicelmx    2020-05-05  
   一只码畜    2020-04-22  
   拉斯厄尔高福    2020-04-18  
   muhongdi    2020-04-18  
   choupiaoyi    2020-04-06  
   wbczyh    2020-03-28  
   jeonkc    2020-03-26  
   狼窝    2020-03-20  
 