排序与另一个数组的数组 [英] Sorting an array related to another array

查看:135
本文介绍了排序与另一个数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个阵列,x和y,其中y是几十x中的每个元素的值。现在,我要排序年。但是,y的顺序将是X的不同。所以,我不能排序,这元素y为相关的实例后告诉,X [0]。我希望有一个双排序的可能。你的帮助是无限AP preciated!

If I have two arrays, x and y where y is the value of the tens of every element in x. Now, I want to sort y. But, the order of y will be different of x's. So, I can't tell after sorting which element in y was related to, for instance, x[0]. I want a "double sorting" may be. Your help is infinitely appreciated!

推荐答案

的Array.Sort 拥有的接受两个阵列过载;一个用于键和一个用于物品。的项目的两个键进行排序数组:

Array.Sort has an overload that accepts two arrays; one for the keys, and one for the items. The items of both are sorted according to the keys array:

int[] keys = { 1, 4, 3, 2, 5 };
string[] items = { "abc", "def", "ghi", "jkl", "mno" };
Array.Sort(keys, items);
foreach (int key in keys) {
    Console.WriteLine(key); // 1, 2, 3, 4, 5
}
foreach (string item in items) {
    Console.WriteLine(item); // abc, jkl, ghi, def, mno
}

所以你的情况,这听起来像你想要的:

So in your case, it sounds like you want:

Array.Sort(y,x); // or Sort(x,y); - it isn't  100% clear

这篇关于排序与另一个数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆