/// <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
 {
     Dictionary.ThrowIfNull("Dictionary");
     OrderBy.ThrowIfNull("OrderBy");
     return(Dictionary.OrderBy(OrderBy, Comparer.NullCheck(new GenericComparer <T3>())).ToDictionary(x => x.Key, x => x.Value));
 }
 /// <summary>
 /// Sorts a dictionary by value
 /// </summary>
 /// <typeparam name="T1">Key type</typeparam>
 /// <typeparam name="T2">Value type</typeparam>
 /// <param name="Dictionary">Dictionary to sort</param>
 /// <param name="Comparer">Comparer used to sort (defaults to GenericComparer)</param>
 /// <returns>The sorted dictionary</returns>
 public static IDictionary <T1, T2> SortByValue <T1, T2>(this IDictionary <T1, T2> Dictionary, IComparer <T1> Comparer = null) where T1 : IComparable
 {
     Dictionary.ThrowIfNull("Dictionary");
     return(new SortedDictionary <T1, T2>(Dictionary, Comparer.NullCheck(new GenericComparer <T1>()))
            .OrderBy(x => x.Value)
            .ToDictionary(x => x.Key, x => x.Value));
 }
示例#3
0
        /// <summary>
        /// 按照一定的规则进行快速排序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="comparer"></param>
        public static void QuickSortList <T>(this IList list, IComparer <T> comparer)
        {
            list.NullCheck("list");
            comparer.NullCheck("comparer");

            Comparison <T> comparison = (left, right) => comparer.Compare(left, right);

            InnerQuickSort(list, 0, list.Count - 1, comparison);
        }
示例#4
0
 /// <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
 {
     Guard.NotNull(dictionary, "dictionary");
     Guard.NotNull(orderBy, "orderBy");
     return
         (dictionary.OrderBy(orderBy, comparer.NullCheck(() => 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.NullCheck(() => new GenericComparer <T>());
     if (comparer.Compare(max, value) < 0)
     {
         return(max);
     }
     if (comparer.Compare(value, min) < 0)
     {
         return(min);
     }
     return(value);
 }
示例#6
0
        /// <summary>
        /// 按照指定的规则进行排序
        /// </summary>
        /// <param name="comparer"></param>
        public void Sort(IComparer <T> comparer)
        {
            comparer.NullCheck("comparer");

            this.List.QuickSortList(comparer);
        }
        /// <summary>
        /// 按照指定的规则进行排序
        /// </summary>
        /// <param name="comparer"></param>
        public void Sort(IComparer <T> comparer)
        {
            comparer.NullCheck("comparer");

            InnerSort(list => list.Sort(comparer));
        }
 /// <summary>
 /// Sorts a dictionary
 /// </summary>
 /// <typeparam name="T1">Key type</typeparam>
 /// <typeparam name="T2">Value type</typeparam>
 /// <param name="Dictionary">Dictionary to sort</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>(this IDictionary <T1, T2> Dictionary, IComparer <T1> Comparer = null) where T1 : IComparable
 {
     Dictionary.ThrowIfNull("Dictionary");
     return(new SortedDictionary <T1, T2>(Dictionary, Comparer.NullCheck(new GenericComparer <T1>())));
 }
示例#9
0
 /// <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.NullCheck(new GenericComparer <T>());
     return(Comparer.Compare(Max, Value) >= 0 && Comparer.Compare(Value, Min) >= 0);
 }
示例#10
0
 /// <summary>
 /// 检查当前的值是否居于两者之间(包括等于)
 /// </summary>
 public static bool Between <T>(this T value, T min, T max, IComparer <T> comparer = null) where T : IComparable
 {
     comparer = comparer.NullCheck(new GenericComparer <T>());
     return(comparer.Compare(max, value) >= 0 && comparer.Compare(value, min) >= 0);
 }
 /// <summary>
 ///     Returns the maximum 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 maximum value</returns>
 public static T Max <T>(this T inputA, T inputB, IComparer <T> comparer = null)
     where T : IComparable
 {
     comparer = comparer.NullCheck(() => new GenericComparer <T>());
     return(comparer.Compare(inputA, inputB) < 0 ? inputB : inputA);
 }