static public string[] SortArray(string[] str, CompareString compareStr) { if (compareStr != null) { string temp; for (int i = 0; i < str.Length; i++) { for (int j = i + 1; j < str.Length; j++) { if (compareStr(str[i], str[j]) != 0) { if (compareStr(str[i], str[j]) == -1) { temp = str[i]; str[i] = str[j]; str[j] = temp; } } } } return(str); } else { throw new ArgumentException("Delegate is null"); } }
static void Main(string[] args) { string[] strArray = { "we", "are", "a", "global", "team", "of", "tecno" }; CompareString compareStr = new CompareString(CompareElemByAlphabet); strArray = SortArray(strArray, compareStr); compareStr = new CompareString(CompareElemByLength); strArray = SortArray(strArray, compareStr); Console.WriteLine(String.Join(" ", strArray)); Console.ReadKey(); }
static void Main(string[] args) { string[] arrayStr = new string[5] { "car", "char", "sharp", "sdr", "rlt" }; CompareString comp = new CompareString(Compare); SortString(arrayStr, comp); for (int i = 0; i < arrayStr.Length; i++) { Console.WriteLine(arrayStr[i]); } Console.ReadKey(); }
static void Main(string[] args) { string[] arrayStr = new string[5] { "lalala", "ese", "ewewe", "feeee", "mur" }; CompareString comp = new CompareString(Compare); SortString(arrayStr, comp); for (int i = 0; i < arrayStr.Length; i++) { Console.WriteLine(arrayStr[i]); } Console.ReadKey(); }
public static void SortString(string[] arrayStr, CompareString comp) { for (int i = 0; i < arrayStr.Length - 1; i++) { for (int j = i + 1; j < arrayStr.Length; j++) { if (comp(arrayStr[i], arrayStr[j])) { string s = arrayStr[i]; arrayStr[i] = arrayStr[j]; arrayStr[j] = s; } } } }