不要用ArrayList,用List<Tuple<int, int>>
代码如下:
List<Tuple<int, int>> list = new List<Tuple<int, int>>();
list.Add(new Tuple<int, int>(120, 2));
list.Add(new Tuple<int, int>(160, 6));
list.Add(new Tuple<int, int>(30, 9));
list.Add(new Tuple<int, int>(80, 10));
foreach (var item in list.OrderByDescending(x => x.Item1))
Console.WriteLine(item.Item2);
代码如下:
List<Tuple<int, int>> list = new List<Tuple<int, int>>();
list.Add(new Tuple<int, int>(120, 2));
list.Add(new Tuple<int, int>(160, 6));
list.Add(new Tuple<int, int>(30, 9));
list.Add(new Tuple<int, int>(80, 10));
foreach (var item in list.OrderByDescending(x => x.Item1))
Console.WriteLine(item.Item2);
for (int i = 0; i < arraylist1.Count(); i++)
list.Add(new Tuple<int, int>(arraylist1[i], arraylist2[i]));
下面接上之前的代码
list1: 120, 160, 30, 80, 30
list2: 2, 6, 9, 10, 8
咋办?
你想做排序,必然是自己用代码去遍历判断,不管是冒泡也好,还是什么也好
你在修改list1的时候,顺便把list2也修改了不就得了
list1: 120, 160, 30, 80
list2: 2, 6, 9, 10
循环判断,第一次修改
list1: 160, 120, 30, 80这时你知道是交互0和1的位置,顺便就把list2的0和1交换过来
list2: 6, 2, 9, 10
继续循环判断,中间不需要交换位置的过程省略,第二次修改
list1: 160, 120, 80, 30此时你知道要交换2,3,就把list2里的2,3也交换了
list2: 6, 2, 10,9
这不就是你想要的吗
可以用KeyValuePair
public void Test()
{
SortedDictionary<int, int> sd = new SortedDictionary<int, int>();
int[] arr1 = new int[] { 120, 160, 30, 80 };
int[] arr2 = new int[] { 2, 6, 9, 10 };
//List<int> list1 = new List<int>(arr1);
//List<int> list2 = new List<int>(arr2);
ArrayList lis1 = new ArrayList(arr1);
ArrayList lis2 = new ArrayList(arr2);
int[] list1 = (int[])lis1.ToArray(typeof(Int32));
int[] list2 = (int[])lis2.ToArray(typeof(Int32));
for(int i=0;i<list1.Length;i++)
{
sd.Add(list1[i], list2[i]);
}
var query = sd.OrderByDescending(t => t.Key).ToDictionary(t => t.Key, t => t.Value);
string str = "";
foreach (int key in query.Keys)
{
str = str + key + " ";
}
foreach (int value in query.Values)
{
str = str + value + " ";
}
MessageBox.Show(str);
}