我现在有两个数组A{2,1,5,4,3}和B{a,b,c,d,e},两个数组元素是一一对应的,当数组A排序后变为{1,2,3,4,5},数组B也变为{b,a,e,d,c}.也就是说,当一个数组改变顺序时,另一个对应的数组也跟着变化!
请问如何实现类似的关联???

解决方案 »

  1.   

    把两个数组的元素都放到一个map里面 变成k,v对然后直接操作map就可以了
      

  2.   


    int[] a = { 2, 1, 5, 4, 3 };
                string[] b = { "a", "b", "c", "d", "e" };
                Dictionary<string, int> dic = new Dictionary<string, int>();
                for (int i = 0; i < a.Length; i++)
                    dic.Add(b[i], a[i]);
                Array.Sort(a);
                Array.Sort(b, new Comparison<string>(delegate(string first, string second)
                    {
                        int indexa = Array.IndexOf(a, dic[first]);
                        int indexb = Array.IndexOf(a, dic[second]);
                        return indexa - indexb;
                    }));
                foreach (int i in a)
                    Console.Write(i + " ");
                Console.WriteLine();
                foreach (string s in b)
                    Console.Write(s + " ");
      

  3.   

    对数组B不能做任何操作,只能操作数组A,数组B要根据初始对应关系跟着变@!
      

  4.   

    晕,这还是需要操作数组B啊,
    实在不行你就申明一个 数组C,存储数组B的值,然后将C与A同时变换,这样当A排好序后,C也排好序了,然后将C覆盖数组B。