public void Sort(T[] keys, int index, int length, IComparer <T> comparer)
 {
     try
     {
         if ((comparer == null) || (comparer == Comparer <T> .Default))
         {
             GenericArraySortHelper <T> .QuickSort(keys, index, index + (length - 1));
         }
         else
         {
             ArraySortHelper <T> .QuickSort(keys, index, index + (length - 1), comparer);
         }
     }
     catch (IndexOutOfRangeException)
     {
         object[] values = new object[3];
         values[0] = default(T);
         values[1] = typeof(T).Name;
         throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", values));
     }
     catch (Exception exception)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
     }
 }
示例#2
0
        internal static void QuickSort(T[] keys, int left, int right, IComparer <T> comparer)
        {
            do
            {
                int a    = left;
                int b    = right;
                int num3 = a + ((b - a) >> 1);
                ArraySortHelper <T> .SwapIfGreaterWithItems(keys, comparer, a, num3);

                ArraySortHelper <T> .SwapIfGreaterWithItems(keys, comparer, a, b);

                ArraySortHelper <T> .SwapIfGreaterWithItems(keys, comparer, num3, b);

                T y = keys[num3];
                do
                {
                    while (comparer.Compare(keys[a], y) < 0)
                    {
                        a++;
                    }
                    while (comparer.Compare(y, keys[b]) < 0)
                    {
                        b--;
                    }
                    if (a > b)
                    {
                        break;
                    }
                    if (a < b)
                    {
                        T local2 = keys[a];
                        keys[a] = keys[b];
                        keys[b] = local2;
                    }
                    a++;
                    b--;
                }while (a <= b);
                if ((b - left) <= (right - a))
                {
                    if (left < b)
                    {
                        ArraySortHelper <T> .QuickSort(keys, left, b, comparer);
                    }
                    left = a;
                }
                else
                {
                    if (a < right)
                    {
                        ArraySortHelper <T> .QuickSort(keys, a, right, comparer);
                    }
                    right = b;
                }
            }while (left < right);
        }
 public void Sort(TKey[] keys, TValue[] values, int index, int length, IComparer <TKey> comparer)
 {
     try
     {
         if ((comparer == null) || (comparer == Comparer <TKey> .Default))
         {
             comparer = Comparer <TKey> .Default;
         }
         ArraySortHelper <TKey, TValue> .QuickSort(keys, values, index, index + (length - 1), comparer);
     }
     catch (IndexOutOfRangeException)
     {
         object[] objArray = new object[3];
         objArray[1] = typeof(TKey).Name;
         objArray[2] = comparer;
         throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", objArray));
     }
     catch (Exception exception)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
     }
 }