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

 

转载于:https://www.cnblogs.com/ants-double/p/6044942.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值