/// <summary> /// Sorts a dictionary /// </summary> /// <typeparam name="T1">Key type</typeparam> /// <typeparam name="T2">Value type</typeparam> /// <typeparam name="T3">Order by type</typeparam> /// <param name="Dictionary">Dictionary to sort</param> /// <param name="OrderBy">Function used to order the dictionary</param> /// <param name="Comparer">Comparer used to sort (defaults to GenericComparer)</param> /// <returns>The sorted dictionary</returns> public static IDictionary <T1, T2> Sort <T1, T2, T3>(this IDictionary <T1, T2> Dictionary, Func <KeyValuePair <T1, T2>, T3> OrderBy, IComparer <T3> Comparer = null) where T3 : IComparable { Contract.Requires <ArgumentNullException>(Dictionary != null, "Dictionary"); Contract.Requires <ArgumentNullException>(OrderBy != null, "OrderBy"); return(Dictionary.OrderBy(OrderBy, Comparer.Check(() => new GenericComparer <T3>())).ToDictionary(x => x.Key, x => x.Value)); }
/// <summary> /// Clamps a value between two values /// </summary> /// <param name="Value">Value sent in</param> /// <param name="Max">Max value it can be (inclusive)</param> /// <param name="Min">Min value it can be (inclusive)</param> /// <param name="Comparer">Comparer to use (defaults to GenericComparer)</param> /// <returns>The value set between Min and Max</returns> public static T Clamp <T>(this T Value, T Max, T Min, IComparer <T> Comparer = null) where T : IComparable { Comparer = Comparer.Check(() => new GenericComparer <T>()); if (Comparer.Compare(Max, Value) < 0) { return(Max); } if (Comparer.Compare(Value, Min) < 0) { return(Min); } return(Value); }
/// <summary> /// Returns the minimum value between the two /// </summary> /// <param name="InputA">Input A</param> /// <param name="InputB">Input B</param> /// <param name="Comparer">Comparer to use (defaults to GenericComparer)</param> /// <returns>The minimum value</returns> public static T Min <T>(this T InputA, T InputB, IComparer <T> Comparer = null) where T : IComparable { Comparer = Comparer.Check(() => new GenericComparer <T>()); return(Comparer.Compare(InputA, InputB) > 0 ? InputB : InputA); }
/// <summary> /// Checks if an item is between two values /// </summary> /// <typeparam name="T">Type of the value</typeparam> /// <param name="Value">Value to check</param> /// <param name="Min">Minimum value</param> /// <param name="Max">Maximum value</param> /// <param name="Comparer"> /// Comparer used to compare the values (defaults to GenericComparer)" /// </param> /// <returns>True if it is between the values, false otherwise</returns> public static bool Between <T>(this T Value, T Min, T Max, IComparer <T> Comparer = null) where T : IComparable { Comparer = Comparer.Check(() => new GenericComparer <T>()); return(Comparer.Compare(Max, Value) >= 0 && Comparer.Compare(Value, Min) >= 0); }