/// <summary> /// This is helper method, which checks whether one or both of the given objects are <c>null</c>, and if so, assigns comparison result appropriate to this <see cref="NullSorting"/> enumeration, and returns <c>false</c>. /// If neither of the given objects are <c>null</c>, this then returns <c>true</c>. /// </summary> /// <param name="nullSortingStrategy">This <see cref="NullSorting"/> strategy.</param> /// <param name="x">The first argument to comparison.</param> /// <param name="y">The second argument to comparison.</param> /// <param name="comparisonResult">This parameter will hold the result of the comparison, if <paramref name="x"/> or <paramref name="y"/> or both are <c>null</c>.</param> /// <returns><c>true</c> if <paramref name="x"/> and <paramref name="y"/> are both non-<c>null</c>; <c>false</c> otherwise.</returns> public static Boolean CheckForNullValues(this NullSorting nullSortingStrategy, Object x, Object y, out Int32 comparisonResult) { Boolean retVal; if (x == null) { retVal = false; comparisonResult = y == null ? 0 : (nullSortingStrategy == NullSorting.NullsFirst ? -1 : 1); } else if (y == null) { retVal = false; comparisonResult = nullSortingStrategy == NullSorting.NullsFirst ? 1 : -1; } else { retVal = true; comparisonResult = 0; } return(retVal); }
internal ComparerWithFunctionAndNullStrategy(Comparison <T> compareFunc, NullSorting nullSorting) { ArgumentValidator.ValidateNotNull("Comparer function", compareFunc); this._compareFunc = compareFunc; this._nullSorting = nullSorting; }
/// <summary> /// Creates a new <see cref="IComparer{T}"/> which behaves as <paramref name="comparison"/> callback specify, and which sorts <c>null</c> values according to given strategy. /// This means that the given callback will never receive <c>null</c> values. /// </summary> /// <typeparam name="T">The type of object being compared.</typeparam> /// <param name="comparison">The function comparing the object, should return same as <see cref="IComparer{T}.Compare(T,T)"/> method.</param> /// <param name="nullSortingStrategy">The strategy to sort <c>null</c> values.</param> /// <returns>A new <see cref="IComparer{T}"/> which behaves as parameters specify.</returns> /// <exception cref="ArgumentNullException">If <paramref name="comparison"/> is <c>null</c>.</exception> /// <remarks>The return value can be casted to <see cref="System.Collections.IComparer"/>.</remarks> public static IComparer <T> NewComparerWithNullStrategy <T>(Comparison <T> comparison, NullSorting nullSortingStrategy) { return(new ComparerWithFunctionAndNullStrategy <T>(comparison, nullSortingStrategy)); }