对与另一个数组相关的数组进行排序 [英] Sorting an array related to another array

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

问题描述

我有两个数组,xy,其中 y 是 x 中每个元素的数十个的值.现在,我想对 y 进行排序.但是,y 的顺序将与 x 的顺序不同.因此,在对 y 中的哪个元素进行排序后,我无法分辨出与 x[0] 相关的元素.

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" maybe.

推荐答案

Array.Sort接受两个数组的重载;一个用于钥匙,一个用于物品.both 的项目按照 keys 数组排序:

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天全站免登陆