C# 根据数组C的排序索引对多个数组进行排序#
现在我想对a进行排序,并使用它的索引对b和c进行排序 例如:C# 根据数组C的排序索引对多个数组进行排序#,c#,sorting,C#,Sorting,现在我想对a进行排序,并使用它的索引对b和c进行排序 例如: int[] a = {120, 60, 50, 40, 30, 20}; int[] b = {12, 29, 37, 85, 63, 11}; int[] c = {30, 23, 90 ,110, 21, 34}; 如何在C#中执行此操作一个选项: sorted a = {20,30,40,50,60,120}; sorted b should be ={ 11,63,85,37,29,12}; and sorted c sho
int[] a = {120, 60, 50, 40, 30, 20};
int[] b = {12, 29, 37, 85, 63, 11};
int[] c = {30, 23, 90 ,110, 21, 34};
如何在C#中执行此操作一个选项:
sorted a = {20,30,40,50,60,120};
sorted b should be ={ 11,63,85,37,29,12};
and sorted c should be = { 34,21,110,90,23,30};
您可以使用一层间接寻址来完成此操作,如下所示:
int[] a = {120, 60, 50, 40, 30, 20};
int[] b = {12, 29, 37, 85, 63, 11};
int[] c = {30, 23, 90 ,110, 21, 34};
var ordered = a.Select((item, index) =>
Tuple.Create(item, b[index], c[index]))
.OrderBy(tuple => tuple.Item1).ToArray();
a = ordered.Select(tuple => tuple.Item1).ToArray();
b = ordered.Select(tuple => tuple.Item2).ToArray();
c = ordered.Select(tuple => tuple.Item3).ToArray();
static void Main(字符串[]args)
{
int[]a={120,60,50,40,30,20};
int[]b={12,29,37,85,63,11};
int[]c={30,23,90,110,21,34};
var index=Enumerable.Range(0,a.Length).OrderBy(i=>a[i]).ToArray();
var temp=新整数[a.长度];
foreach(新[]{a,b,c}中的var arr)
{
对于(inti=0;i
这并不是最好的解决方案(我相信您可以以某种方式摆脱临时数组),但却是另一种解决方案。在性能成为问题之前,我支持坚持使用LINQesque解决方案。您可以使用SortedList进行此操作。SortedList使用键对项目进行排序。它还允许您 向收藏中添加新项目
static void Main(string[] args)
{
int[] a = { 120, 60, 50, 40, 30, 20 };
int[] b = { 12, 29, 37, 85, 63, 11 };
int[] c = { 30, 23, 90, 110, 21, 34 };
var indexes = Enumerable.Range(0, a.Length).OrderBy(i => a[i]).ToArray();
var temp = new int[a.Length];
foreach (var arr in new[] { a, b, c })
{
for (int i = 0; i < a.Length; i++) temp[i] = arr[indexes[i]];
for (int i = 0; i < a.Length; i++) arr[i] = temp[i];
}
Console.WriteLine(String.Join(", ", a));
Console.WriteLine(String.Join(", ", b));
Console.WriteLine(String.Join(", ", c));
Console.ReadLine();
}
我建议使用LINQ,因为Eli Arbel提供了答案。 但对于那些不了解LINQ的人来说,这里有另一个解决方案
SortedList Class:
Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
类程序
{
公共静态int get_键(int键,int[,]键列表)
{
对于(int i=0;i您必须有三个数组吗?通常,这里更好的方法是使用单个数组,其中每个元素由三个值组成。这通常更干净,因为这些值可能相互关联。我尝试使用键和值进行排序..如array.Sort(a,c)和array.Sort(a,b)…但是我得到了不想要的结果,例如,在执行Array.Sort(a,b)之后,我得到了两次Enumerable.Zip,OrderBy,然后再次拆分
,a
和b
将不同步,因此您将搞乱关系。您的所有数组是否包含相同数量的元素?@user254634string.Join将不使用int-array进行计算。@NPSF3000您的逻辑正确,但请说明为什么Console.WriteLine(String.Join(“,”,a));给我2个编译时错误。错误1与“string.Join(string,string[])”匹配的最佳重载方法有一些无效参数错误2参数“2”:无法从“int[]”转换为“string[]”@Rezoan我猜您使用的是.net 3.5(或更早版本)框架…或者mono?请参阅获取字符串。Join我使用的是版本3。5@RezoanString.Join是在4.0中添加的。
class Program
{
public static int get_key(int key , int [,] keylist)
{
for (int i = 0; i <= keylist.GetUpperBound(0); ++i)
{
if (keylist[i, 0] == key)
return keylist[i, 1];
}
return -1;
}
public static int[] Sort_by_index(int [] arr , int [] key , int [,] index_list)
{
int[] _out = new int[arr.Length];
for (int i = 0; i < key.Length; i++)
{
//Get key index
int key_index = get_key(key[i], index_list);
_out[i] = arr[key_index];
}
return _out;
}
static void Main(string[] args)
{
int[] a = { 120, 60, 50, 40, 30, 20 };
int[] b = { 12, 29, 37, 85, 63, 11 };
int[] c = { 30, 23, 90, 110, 21, 34 };
int[,] a_index = { { 120, 0 }, { 60, 1 }, { 50, 2 }, { 40, 3 }, { 30, 4 }, { 20, 5 } };
Array.Sort(a);
b =Sort_by_index(b, a, a_index);
c =Sort_by_index(c, a, a_index);
Console.WriteLine("Result A");
Console.WriteLine(string.Join(", ",a));
Console.WriteLine("Result B");
Console.WriteLine(string.Join(", ",b));
Console.WriteLine("Result C");
Console.WriteLine(string.Join(", ",c));
Console.ReadKey(false);
}
}