英文:
Go: Sort array, dropping elements if error caught in `Less(i, j int)`
问题
给定以下的struct
:
type Point struct {
datetimeRecorded time.Time
}
// 如果点在比较点之前记录,则返回true。
// 如果没有可用的日期时间,则返回false和错误。
func (p1 Point) RecordedBefore(p2 Point) (isBefore bool, err error) {
if (p1.datetimeRecorded.IsZero()) || (p2.datetimeRecorded.IsZero()) {
err = ErrNoDatetime
} else {
isBefore = p1.datetimeRecorded.Before(p2.datetimeRecorded)
}
return
}
我想通过datetimeRecorded
属性对[]Point
进行排序。
我有以下代码(可以工作):
type ByDatetimeRecorded []Point
func (a ByDatetimeRecorded) Len() int {
return len(a)
}
func (a ByDatetimeRecorded) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByDatetimeRecorded) Less(i, j int) bool {
swap, _ := a[i].RecordedBefore(a[j])
return swap
}
然而,如果datetimeRecorded
属性在比较中没有被初始化,会捕获到一个error
,并且点不会被交换(返回false
)。
是否可能捕获此错误并将其从数组中删除?类似于:
func (a ByDatetimeRecorded) Less(i, j int) bool {
swap, err := a[i].RecordedBefore(a[j])
if err != nil {
// 在这里删除元素
}
return swap
}
编辑1
我可能需要更具体地说明要删除的元素,这样可能更有意义:
func (a ByDatetimeRecorded) Less(i, j int) bool {
if a[i].datetimeRecorded.IsZero() {
// 删除a[i]
}
if a[j].datetimeRecorded.IsZero() {
// 删除a[j]
}
swap, _ := a[i].RecordedBefore(a[j])
return swap
}
英文:
Given the following struct
type Point struct {
datetimeRecorded time.Time
}
// Returns true if the point was recorded before the comparison point.
// If datetime is not available return false and an error
func (p1 Point) RecordedBefore(p2 Point) (isBefore bool, err error) {
if (p1.datetimeRecorded.IsZero()) || (p2.datetimeRecorded.IsZero()) {
err = ErrNoDatetime
} else {
isBefore = p1.datetimeRecorded.Before(p2.datetimeRecorded)
}
return
}
I would like to sort a []Point
by the datetimeRecorded
attribute.
I have the following (which works):
type ByDatetimeRecorded []Point
func (a ByDatetimeRecorded) Len() int {
return len(a)
}
func (a ByDatetimeRecorded) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByDatetimeRecorded) Less(i, j int) bool {
swap, _ := a[i].RecordedBefore(a[j])
return swap
}
However, if the datetimeRecorded
attribute is not initialised in either comparison an error
will be caught and the points not swapped (returns false
).
Is it possible to catch this error and drop it from the array? Something like:
func (a ByDatetimeRecorded) Less(i, j int) bool {
swap, err := a[i].RecordedBefore(a[j])
if err != nil {
// Remove element here
}
return swap
}
Edit 1
I may have to be more specific about what element to drop so this may make more sense:
func (a ByDatetimeRecorded) Less(i, j int) bool {
if a[i].datetimeRecorded.IsZero() {
// Drop a[i]
}
if a[j].datetimeRecorded.IsZero() {
// Drop a[j]
}
swap, _ := a[i].RecordedBefore(a[j])
return swap
}
答案1
得分: 2
标准的排序包不会从切片中删除元素。
在排序之前,先将切片中的零值过滤掉。
i := 0
for _, p := range points {
if !p.datetimeRecorded.IsZero() {
points[i] = p
i++
}
}
points = points[:i]
sort.Sort(ByDatetimeRecorded(points))
英文:
The standard sort package will not drop elements from the slice.
Filter the zero values out of the slice before sorting.
i := 0
for _, p := range points {
if !p.datetimeRecorded.IsZero() {
points[i] = p
i++
}
}
points = points[:i]
sort.Sort(ByDatetimeRecorded(points))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论