今天想把兩個數組關聯起來再排序,本身寫了半天忽然發現有現成的東西可使用。數組
public static void Sort( Array keys, Array items, IComparer comparer )
public static void Sort<TKey, TValue>( TKey[] keys, TValue[] items, int index, int length )
static void Main(string[] args) { double[] keys = { 4, 5, 6, 22, 3343, 2 }; double[] values = { 4, 1, 324, 5, 3, 2 }; Array.Sort(keys, values, 1, 4); PrintKeysAndValues(keys, values); Console.Read(); } public static void PrintKeysAndValues(double[] myKeys, double[] myValues) { for (int i = 0; i < myKeys.Length; i++) { Console.WriteLine(" {0,-10}: {1}", myKeys[i], myValues[i]); } Console.WriteLine(); }
// Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare(Object x, Object y) { // return ((new CaseInsensitiveComparer()).Compare(y, x)); //if (double.Parse(x.ToString()) %2==0) //{ // return 1; //} //else //{ // return 0; //} if ((new CaseInsensitiveComparer()).Compare(y, x) > 0) { return 1; } else { return -1; } } } class Program { static void Main(string[] args) { IComparer myComparer = new myReverserClass(); // String[] myKeys = { "red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange" }; // String[] myValues = { "strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe" }; double[] myKeys = { 2, 34, 4, 5, 6, 6 }; double[] myValues = { 1, 2, 3, 555, 5, 6, 7 }; Array.Sort(myKeys, myValues, myComparer); Console.WriteLine("The Array initially contains the following values:"); PrintKeysAndValues(myKeys, myValues); Console.Read(); } public static void PrintKeysAndValues(double[] myKeys, double[] myValues) { for (int i = 0; i < myKeys.Length; i++) { Console.WriteLine(" {0,-10}: {1}", myKeys[i], myValues[i]); } Console.WriteLine(); }