Array.sort的用法

今天想把两个数组关联起来再排序,自己写了半天突然发现有现成的东西可以使用。

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();
        }

 

posted @ 2016-11-08 22:19  lixin08  阅读(320)  评论(0编辑  收藏  举报