public void SortArray <T>(T[] array, ComparisonDel <T> comparison, Action <string> callback) where T : IComparable <T> { T temp = default(T); string arrayType = array.ToString(); if (comparison != null) { for (int i = 0; i < array.Length; i++) { for (int j = i + 1; j < array.Length; j++) { if (comparison(array[i], array[j])) { temp = array[i]; array[i] = array[j]; array[j] = temp; } Console.WriteLine($" - - - В массиве {arrayType} произошло сравнение {array[i]} и {array[j]}"); Thread.Sleep(50); } } } callback?.Invoke(arrayType); }
//Variant 2: Callback signals about the end of the sorting private static void ShowSortWithCallback(ComparisonDel <int> compInt, ComparisonDel <double> compDouble, ComparisonDel <string> compString) { SortingUnit2 sortUnit = new SortingUnit2(); sortUnit.SortInNewThread(arrayInt, compInt); sortUnit.SortInNewThread(arrayDouble, compDouble); sortUnit.SortInNewThread(arrayString, compString); }
public void SortInNewThread <T>(T[] array, ComparisonDel <T> comparison) where T : IComparable <T> { Thread newThread = new Thread( () => SortArray(array, comparison, SortEndHandler)); Console.WriteLine($"Запускается поток для сортировки массива {array.ToString()}, состоящего из {array.Length} элементов:"); newThread.Start(); }
static void SortingUnitDemo() { ComparisonDel <int> comparisonInt = Comparison; ComparisonDel <double> comparisonDouble = Comparison; ComparisonDel <string> comparisonString = Comparison; Console.WriteLine("{0} * * * ОБ ОКОНЧАНИИ СОРТИРОВКИ СИГНАЛИЗИРУЕТ СОБЫТИЕ * * *{0}", Environment.NewLine); ShowSortWithEvent(comparisonInt, comparisonDouble, comparisonString); Console.WriteLine("{0} * * * УЗНАЕМ ОБ ОКОНЧАНИИ СОРТИРОВКИ ЧЕРЕЗ CALLBACK * * *{0}", Environment.NewLine); ShowSortWithCallback(comparisonInt, comparisonDouble, comparisonString); }
static void CustomSortDemo() { string[] array = new string[] { "Мадрид", "Осло", "Сеул", "Москва", "Афины", "Рим", "Лиссабон", "Рига", "Шри-Джаяварденепура-Котте", "Баку" }; Console.WriteLine("{0}Исходный массив:{0}", Environment.NewLine); Show(array); ComparisonDel <string> comparisonDelegate = ComparisonStrings; SortArray(array, comparisonDelegate); Console.WriteLine("{0}{0}Отсортированный массив:{0}", Environment.NewLine); Show(array); ToNext(); }
static void CustomSort() { int[] array = new int[] { 10, 2, 5, 4, 3, 4, 8, 9, 1, 99, 25, 40, 3 }; Console.WriteLine("Исходный массив:{0}", Environment.NewLine); Show(array); ComparisonDel <int> comparisonDelegate = Comparison; SortArray(array, comparisonDelegate); Console.WriteLine("{0}{0}Отсортированный массив:{0}", Environment.NewLine); Show(array); ToNext(); }
//Variant 1: The event signals about the end of the sorting private static void ShowSortWithEvent(ComparisonDel <int> compInt, ComparisonDel <double> compDouble, ComparisonDel <string> compString) { SortingUnit sortUnit = new SortingUnit(); Handler handler = new Handler(); sortUnit.OnSorted += handler.Message; sortUnit.SortInNewThread(arrayInt, compInt); sortUnit.SortInNewThread(arrayDouble, compDouble); sortUnit.SortInNewThread(arrayString, compString); sortUnit.OnSorted -= handler.Message; sortUnit.SortInNewThread(arrayString, compString); ToNext(); }
static void SortArray <T>(T[] array, ComparisonDel <T> comparison) where T : IComparable <T> { T temp = default(T); if (comparison != null) { for (int i = 0; i < array.Length; i++) { for (int j = i + 1; j < array.Length; j++) { if (comparison(array[i], array[j])) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } }