/// <summary> /// Finds the max item in the collection. if max items are repeted then it /// will return first one /// return the first one /// </summary> public static T FindMax <T>(IEnumerable <T> collection, ComparerFunc <T> comparer) { var enumerator = collection.GetEnumerator(); T max; if (enumerator.MoveNext()) { max = enumerator.Current; while (enumerator.MoveNext()) { T t = enumerator.Current; if (comparer(t, max) > 0) { // t is greater than max, update max max = t; } } } else { throw new ApplicationException("not a valid collection"); } return(max); }
public static List <TElement> Sort <TElement, TKey>(this List <TElement> list, Func <TElement, TKey> key) where TKey : IComparable <TKey> { var comparer = new ComparerFunc <TElement>((x, y) => key(x).CompareTo(key(y))); list.Sort(comparer); return(list); }
/// <summary> /// Finds the min item in the collection. if min items are repeated then it /// will return first one /// </summary> public static T FindMin <T>(IEnumerable <T> collection, ComparerFunc <T> comparer) { IEnumerator <T> enumerator = collection.GetEnumerator(); T min; if (enumerator.MoveNext()) { min = enumerator.Current; while (enumerator.MoveNext()) { T t = enumerator.Current; if (comparer(t, min) < 0) { // t is less that than min, update max min = t; } } } else { throw new ApplicationException("Not a valid collection"); } return(min); }
public void SortWithFuncDelegate(int[] firstRow, int[] secondRow, int[] thirdRow, int[] fouthRow) { int[][] actual = new int[4][]; actual[0] = fouthRow; actual[1] = thirdRow; actual[2] = firstRow; actual[3] = secondRow; ComparerSumIncrease sumInc = new ComparerSumIncrease(); Func <int[], int[], int> predicate = (sumInc.Compare); ComparerFunc comparerFunc = new ComparerFunc(predicate); BubbleSort(actual, comparerFunc); int[][] expected = new int[4][]; expected[0] = thirdRow; expected[1] = secondRow; expected[2] = fouthRow; expected[3] = firstRow; BubbleSort(actual, predicate); }
public void TestOrderByComparerPreserved() { var sequence = Enumerable.Range(1, 100); var sequenceAscending = sequence.Select(x => x.ToString()); var sequenceDescending = sequenceAscending.Reverse(); var comparer = new ComparerFunc<string>((a, b) => int.Parse(a).CompareTo(int.Parse(b))); var resultAsc1 = sequenceAscending.OrderBy(x => x, comparer, OrderByDirection.Descending); var resultAsc2 = sequenceAscending.OrderByDescending(x => x, comparer); // ensure both order by operations produce identical results Assert.IsTrue(resultAsc1.SequenceEqual(resultAsc2)); // ensure comparer was applied in the order by evaluation Assert.IsTrue(resultAsc1.SequenceEqual(sequenceDescending)); var resultDes1 = sequenceDescending.OrderBy(x => x, comparer, OrderByDirection.Ascending); var resultDes2 = sequenceDescending.OrderBy(x => x, comparer); // ensure both order by operations produce identical results Assert.IsTrue(resultDes1.SequenceEqual(resultDes2)); // ensure comparer was applied in the order by evaluation Assert.IsTrue(resultDes1.SequenceEqual(sequenceAscending)); }
public int Compare_Absolute_Tests(int a, int b, int c, int d, int e) { Func <int, int, int> absoluteComparerFunc = (x, y) => { if (Math.Abs(x) > Math.Abs(y)) { return(1); } if (Math.Abs(x) < Math.Abs(y)) { return(-1); } return(0); }; var list = new[] { a, b, c, d, e }; var sortedList = list.OrderBy(z => z, ComparerFunc <int> .Create(absoluteComparerFunc)) .ToList(); return(sortedList.Last()); }
public string Compare_OrderBy_Tests(int a, int b, int c, int d, int e) { Func <int, int, int> evensFirst = (x, y) => { if (x == y) { return(0); } if (x % 2 == y % 2) { return(x < y ? -1 : 1); } return((x % 2 == 0) ? -1 : 1); }; var list = new[] { a, b, c, d, e }; var sortedList = list.OrderBy(z => z, ComparerFunc <int> .Create(evensFirst)) .ToList(); return(string.Join(",", sortedList.Select(x => x.ToString()))); }
Comparer(ComparerFunc f) { this.cmp = f; }
Comparer (ComparerFunc f) { this.cmp = f; }
public void TestThenByComparerPreserved() { var sequence = new[] { new {A = "2", B = "0"}, new {A = "1", B = "5"}, new {A = "2", B = "2"}, new {A = "1", B = "3"}, new {A = "1", B = "4"}, new {A = "2", B = "1"}, }; var comparer = new ComparerFunc<string>((a, b) => int.Parse(a).CompareTo(int.Parse(b))); var resultA1 = sequence.OrderBy(x => x.A, comparer, OrderByDirection.Ascending) .ThenBy(y => y.B, comparer, OrderByDirection.Ascending); var resultA2 = sequence.OrderBy(x => x.A, comparer) .ThenBy(y => y.B, comparer); // ensure both produce the same order Assert.IsTrue(resultA1.SequenceEqual(resultA2)); var resultB1 = sequence.OrderBy(x => x.A, comparer, OrderByDirection.Ascending) .ThenBy(y => y.B, comparer, OrderByDirection.Descending); var resultB2 = sequence.OrderBy(x => x.A, comparer) .ThenByDescending(y => y.B, comparer); // ensure both produce the same order Assert.IsTrue(resultB1.SequenceEqual(resultB2)); }