Example #1
0
 /// <summary>
 /// Sorts the elements of a sequence in ascending order by using a specified comparer delegate.
 /// </summary>
 /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
 /// <param name="source">A sequence of values to order.</param>
 /// <param name="keySelector">A function to extract a key from an element.</param>
 /// <param name="comparer">A comparer delegate to compare keys.</param>
 /// <returns>An IOrderedEnumerable&lt;TElement> whose elements are sorted according to a key.</returns>
 public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey>(
     this IEnumerable <TSource> source,
     Func <TSource, TKey> keySelector,
     ComparerDelegate <TKey> comparer)
 {
     return(source.OrderBy(keySelector, new DelegateComparer <TKey>(comparer)));
 }
Example #2
0
 /// <summary>
 /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer delegate.
 /// </summary>
 /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
 /// <param name="source">An IOrderedEnumerable&lt;TSource> that contains elements to sort.</param>
 /// <param name="keySelector">A function to extract a key from each element.</param>
 /// <param name="comparer">A comparer delegate to compare keys.</param>
 /// <returns>An IOrderedEnumerable&lt;TSource> whose elements are sorted in descending order according to a key.</returns>
 public static IOrderedEnumerable <TSource> ThenByDescending <TSource, TKey>(
     this IOrderedEnumerable <TSource> source,
     Func <TSource, TKey> keySelector,
     ComparerDelegate <TKey> comparer)
 {
     return(source.ThenByDescending(keySelector, new DelegateComparer <TKey>(comparer)));
 }
Example #3
0
        // Sort with giver comparer degegate
        public static int[][] Sort(int[][] array, ComparerDelegate comparerDelegate, bool reverse = false)
        {
            if (array == null)
            {
                throw new ArgumentException();
            }

            int[][] result = (int[][])array.Clone();
            for (int i = 0; i < array.Length; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (!reverse)
                    {
                        if (comparerDelegate(result[i], result[j]) > 0)
                        {
                            SwapIntArrs(ref result[i], ref result[j]);
                        }
                    }
                    else
                    {
                        if (comparerDelegate(result[i], result[j]) < 0)
                        {
                            SwapIntArrs(ref result[i], ref result[j]);
                        }
                    }
                }
            }

            return(result);
        }
Example #4
0
        /// <summary>
        /// Creates new instance of the class with the specified delegate.
        /// </summary>
        /// <param name="comparer">A delegate to compare two objects.</param>
        public DelegateComparer(ComparerDelegate <T> comparer)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            this.comparer = comparer;
        }
 public SortedArray(ComparerDelegate comparer, T[] arr)
 {
     num = count;
     count++;
     this.comparer = comparer.Invoke();
     this.arr      = arr;
     FinishSort   += PrintMessage;
     FinishPrint  += PrintMessage;
 }
Example #6
0
        /// <summary>
        /// Sorts the given array Ascending or Descending
        /// </summary>
        /// <param name="array">Array</param>
        /// <param name="order">Order</param>
        public void Sort(T[] array, SortingOrder order)
        {
            switch (order)
            {
            case SortingOrder.Ascending:
                Comparer = AscendingComparer;
                break;

            case SortingOrder.Descending:
                Comparer = DescendingComparer;
                break;
            }

            Sort(array);
        }
Example #7
0
        private Test CreateComparisonTest <TResult>(ComparisonSpecifications <TResult> spec)
        {
            return(new TestCase(spec.Name, () => Assert.Multiple(() =>
            {
                foreach (InstancePair pair in GetAllPairs())
                {
                    if (IsTargetTypeOrDerived(pair.First))
                    {
                        Type workingType = spec.GetWorkingType(pair.First);

                        if (workingType != null)
                        {
                            MethodInfo methodInfo = spec.GetComparisonMethod(workingType);

                            if (MethodExists(methodInfo, spec.MethodFriendlyName, spec.IsComparisonMethodRequired(pair.First)))
                            {
                                ComparerDelegate <TResult> comparer = GetBinaryComparer <TResult>(methodInfo);

                                if (pair.IsNewFirst)
                                {
                                    if (methodInfo.IsStatic && IsReferenceType(pair.First) && IsReferenceType(pair.Second))
                                    {
                                        VerifyComparison <TResult>(spec, comparer, methodInfo, new InstancePair(null, null, 0));
                                    }

                                    if (IsReferenceType(pair.Second))
                                    {
                                        VerifyComparison <TResult>(spec, comparer, methodInfo, new InstancePair(pair.First, null, 1));
                                    }
                                }

                                if (spec.IsSecondArgumentCompatible(pair.Second))
                                {
                                    VerifyComparison <TResult>(spec, comparer, methodInfo, pair);

                                    if (methodInfo.IsStatic && IsReferenceType(pair.First))
                                    {
                                        VerifyComparison <TResult>(spec, comparer, methodInfo, new InstancePair(null, pair.Second, -1));
                                    }
                                }
                            }
                        }
                    }
                }
            })));
        }
        public static IList<int> BubbleSort(this IList<int> list, ComparerDelegate comparer)
        {
            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = 0; j < list.Count - 1 - i; j++)
                {
                    if (comparer(list[j], list[j + 1]))
                    {
                        int temp = list[j];
                        list[j] = list[j + 1];
                        list[j + 1] = temp;
                    }
                }
            }

            return list;
        }
 public static List<int> BubbleSort(List<int> list, ComparerDelegate comparer)
 {
     for (int i = 0; i < list.Count; i++)
     {
         for (int j = i+1; j < list.Count; j++)
         {
             if (comparer(list[i], list[j]) == 1)
             {
                 int temp;
                 temp = list[i];
                 list[i] = list[j];
                 list[j] = temp;
             }
         }
     }
     return list;
 }
Example #10
0
 /// <summary>
 /// Set the comparer that we'll use to do comparisons
 /// </summary>
 /// <param name="comparer">Class that implements IComparable<T></param>
 public void SetComparer(ComparerDelegate comparer)
 {
     Comparer = comparer;
 }
Example #11
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public SortBase()
 {
     //Sets the default comparer to ascending
     Comparer = AscendingComparer;
 }
Example #12
0
 public static Rule Create(string ruleName, ComparerDelegate comparer)
 {
     return(Create(ruleName, new[] { comparer }));
 }
Example #13
0
        private void VerifyComparison <TResult>(ComparisonSpecifications <TResult> spec, ComparerDelegate <TResult> comparer, MethodInfo methodInfo, InstancePair pair)
        {
            string actual   = spec.FormatResult(comparer(pair.First, pair.Second));
            string expected = spec.FormatResult(spec.AdjustExpectedEquivalence(pair.Equivalence));

            AssertionHelper.Explain(() =>
                                    Assert.AreEqual(expected, actual),
                                    innerFailures => new AssertionFailureBuilder("The comparison between left and right values did not produce the expected result.")
                                    .AddRawLabeledValue("Left Value", pair.First)
                                    .AddRawLabeledValue("Right Value", pair.Second)
                                    .AddRawLabeledValue("Expected Result", expected)
                                    .AddRawLabeledValue("Actual Result", actual)
                                    .SetStackTrace(Context.GetStackTraceData())
                                    .AddInnerFailures(innerFailures)
                                    .ToAssertionFailure());
        }