Go/Golang对指向结构的指针切片进行排序
如何对指向结构的指针切片进行排序。我试图根据开始时间对切片进行排序Go/Golang对指向结构的指针切片进行排序,go,Go,如何对指向结构的指针切片进行排序。我试图根据开始时间对切片进行排序 /** * Definition for an Interval. * type Interval struct { * Start int * End int * } */ func employeeFreeTime(schedule [][]*Interval) []*Interval { fmt.Println("Schedule initial #"
/**
* Definition for an Interval.
* type Interval struct {
* Start int
* End int
* }
*/
func employeeFreeTime(schedule [][]*Interval) []*Interval {
fmt.Println("Schedule initial #", schedule)
sort.Slice(schedule, func(i,j int) bool{
return schedule[i].Start < schedule[j].Start
})
fmt.Println(schedule)
return nil
}
/**
*间隔的定义。
*类型区间结构{
*起始整数
*结束整型
* }
*/
func员工自由时间(计划[][]*间隔)[]*间隔{
fmt.Println(“附表首字母#”,附表)
sort.Slice(schedule,func(i,j int)bool{
返回计划[i]。开始<计划[j]。开始
})
fmt.Println(附表)
归零
}
发送一个切片,而不是一片切片,您可以很好地排序:
/**
* Definition for an Interval.
*/
type Interval struct {
Start int
End int
}
func employeeFreeTime(schedule []*Interval) []*Interval {
fmt.Println("Schedule initial #", schedule)
sort.Slice(schedule, func(i,j int) bool{
return schedule[i].Start < schedule[j].Start
})
fmt.Println(schedule)
return nil
}
func main() {
intervals := []*Interval {
{
Start: 10,
End: 100,
},
{
Start: 5,
End: 100,
},
}
employeeFreeTime(intervals)
}
/**
*间隔的定义。
*/
类型区间结构{
起始整数
结束整型
}
func员工自由时间(计划[]*间隔)[]*间隔{
fmt.Println(“附表首字母#”,附表)
sort.Slice(schedule,func(i,j int)bool{
返回计划[i]。开始<计划[j]。开始
})
fmt.Println(附表)
归零
}
func main(){
间隔:=[]*间隔{
{
开始时间:10,
完:100,,
},
{
开始时间:5,
完:100,,
},
}
员工自由时间(间隔)
}
以防您要对切片中的所有间隔进行排序
func employeeFreeTime(计划[][]*时间间隔)[]*时间间隔{
var tempSlice[]*间隔
对于u,切片:=范围计划{
tempSlice=附加(tempSlice,slice…)
}
sort.Slice(tempSlice,func(i,j int)bool{
返回tempSlice[i]。Start
计划
不是指针的一部分,而是指针的一部分。你想排序什么?我想根据星号排序我明白,但是排序哪个片?你不是只有一个切片,而是可能有很多(一片切片)。