/// <summary>Sorts jagged integer array using method for particular criterion in particular order.</summary> /// <param name="array"> An array to be sorted.</param> /// <param name="comparer"> Interface setting the logic of particular kind of sorting.</param> public static void Sort(int[][] array, IComparer <int[], int[]> comparer) { if (comparer == null) { throw new ArgumentException(nameof(comparer)); } else { int i = 0; bool flag = true; while (flag) { flag = false; for (int j = 0; j < array.Length - i - 1; j++) { if (comparer.CompareTo(array[j], array[j + 1])) { Swap(ref array[j], ref array[j + 1]); flag = true; } } i++; } } }
/// <summary> /// Bubble sorts jugged array using Icomparer /// </summary> /// <param name="array">array</param> /// <param name="ic">comparer privides comparing logic</param> public static void Sort(int[][] array, IComparer ic) { ValidateArray(array); if (ic == null) { throw new ArgumentNullException(); } for (int i = 0; i < array.Length - 1; i++) { for (int j = i + 1; j < array.Length; j++) { if (ic.CompareTo(array[i], array[j]) == 1) { Swap(ref array[i], ref array[j]); } } } }
public int CompareTo(FetchedStudent x, FetchedStudent y) { // TODO: decide what to do with null students: exception? // or return as smallest or largest // Case 1: check if x is in sorting group 1 if (x.Message == null && x.Status == notActive) { // x is in sorting group 1 if (y.Message == null && y.Status == notActive) { // x and y are in sorting group 1. // order by descending UserId return -UserIdComparer.CompareTo(x.UserId, y.UserId); // the minus sign is because of the descending } else { // x is in group 1, y in group 2 / 3 / 4: x comes first return -1; } } // case 2: check if X is in sorting group 2 else if (x.Message != null && x.Status != notActive) { // x is in sorting group 2 if (y.Message == null && y.Status != notActive) { // x is in group 2; y is in group 1: x is larger than y return +1; } else if (y.Message == null && y.Status != notActive) { // x and y both in group 2: order by descending nextFollowUpDate // minus sign is because descending return -nextFollowUpdateComparer.CompareTo( x.Message.NextFollowUpdate, y.Message.NextFollowUpdate); } else { // x in group 2, y in 3 or 4: x comes first return -1; } } // case 3: check if X in sorting group 3 else if (x.Message == null && x.Status != notActive) {