// When sorting a pointer type object slice, the Less method needs to be written. The first part
// is to determine whether the object indicated by the i and j is nil. Nil is considered to be
// the smallest. If both are nil, they are considered to be equal. Only when both of them are not
// nil, the subsequent comparison is needed, and there is no need to check nil at this time.
// When needCompare returns false, the Less method can directly return the returned less value;
// otherwise, the comparison of the indexed objects needs to continue
When sorting a pointer type object slice, the Less method needs to be written. The first part
is to determine whether the object indicated by the i and j is nil. Nil is considered to be
the smallest. If both are nil, they are considered to be equal. Only when both of them are not
nil, the subsequent comparison is needed, and there is no need to check nil at this time.
When needCompare returns false, the Less method can directly return the returned less value;
otherwise, the comparison of the indexed objects needs to continue
// When sorting a pointer type object slice the Less method needs to be written . The first part // is to determine whether the object indicated by the i and j is nil . Nil is considered to be // the smallest . If both are nil they are considered to be equal . Only when both of them are not // nil the subsequent comparison is needed and there is no need to check nil at this time . // When needCompare returns false the Less method can directly return the returned less value ; // otherwise the comparison of the indexed objects needs to continue
func PointerSliceLess(slice interface{}, i, j int) (less bool, needCompare bool) {
	if slice == nil {
		return false, false
	}
	val := reflect.ValueOf(slice)
	if val.Kind() != reflect.Slice {
		return false, false
	}
	if i == j || i < 0 || j < 0 || val.IsNil() || val.Len() <= i || val.Len() <= j {
		return false, false
	}
	vali, valj := val.Index(i), val.Index(j)
	if vali.Kind() != reflect.Ptr || valj.Kind() != reflect.Ptr {
		return false, false
	}
	if vali.IsNil() && valj.IsNil() {
		return false, false
	}
	if vali.IsNil() {
		return true, false
	}
	if valj.IsNil() {
		return false, false
	}
	return false, true
}
func PointerSliceLess ( slice interface { } , i , j int ) ( less bool , needCompare bool ) { if slice == nil { return false , false } val := reflect . ValueOf ( slice ) if val . Kind ( ) != reflect . Slice { return false , false } if i == j || i < 0 || j < 0 || val . IsNil ( ) || val . Len ( ) <= i || val . Len ( ) <= j { return false , false } vali , valj := val . Index ( i ) , val . Index ( j ) if vali . Kind ( ) != reflect . Ptr || valj . Kind ( ) != reflect . Ptr { return false , false } if vali . IsNil ( ) && valj . IsNil ( ) { return false , false } if vali . IsNil ( ) { return true , false } if valj . IsNil ( ) { return false , false } return false , true }
When sorting a pointer type object slice, the Less method needs to be written.
When sorting a pointer type object slice the Less method needs to be written .