0
에 걸린 경우 요소를 떨어지는 struct
이동 : 정렬 배열, 오류가 주어`이하 (I, J의 INT)`
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
}
다음은 내가 datetimeRecorded
속성에 의해 []Point
를 정렬하고 싶습니다.
내가 가지고있는 (어떤 작품) 다음을 수행하십시오 datetimeRecorded
속성이 비교 중 하나에서 초기화되지 않은 경우
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
}
은 그러나, error
캐치 포인트가 (반환 false
)를 교환되지 않습니다.
이 오류를 잡아서 배열에서 삭제할 수 있습니까? 뭔가 같은 :
func (a ByDatetimeRecorded) Less(i, j int) bool {
swap, err := a[i].RecordedBefore(a[j])
if err != nil {
// Remove element here
}
return swap
}
편집 한
내가 지금이 더 이해 할 수 드롭하는 어떤 요소에 대한 구체적 할 수 있습니다 :
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
}
두 단계로 수행하십시오. 잘못된 항목을 제거하십시오. culled 슬라이스 정렬. 그렇지 않으면 한 가지 기능으로 두 가지 일을하고 있으며 이는 좋은 생각이 아닙니다. – RickyA
우수 아이디어 2 부분 - 감사합니다! – Ewan