static void Main(string[] args) { string[] arrayString = new string[] { "зима", "весна", "лето", "осень", "ёж", "модернизация", "Осень", "палата", "ток", "обвал", "кот", }; PrintArray(arrayString); CompareTwoObjects <string> compareString = CompareStringByLength; SortArray(arrayString, compareString); PrintArray(arrayString); Console.WriteLine("---------------------------------------------------------------------"); int[] arrayInt = new int[] { 98, 34, 8, -3, 873, -23, 54, 293, 0, -87, 395 }; PrintArray(arrayInt); SortArray(arrayInt, new CompareTwoObjects <int>(CompareInt)); PrintArray(arrayInt); }
private static void SortArray <T>(T[] array, CompareTwoObjects <T> compareResult) { if (compareResult != null) { for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array.Length - 1; j++) { if (compareResult(array[j], array[j + 1]) > 0) { T temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } else { Console.WriteLine("Comparison principle is not define"); } }