使用C#,我有一个包含对象的2D Jagged数组,我想要实现的是根据这些对象中的公共属性对这个Jagged数组进行排序 我在我的问题下面创建了一个示例, 我使用LINQ的经验有限,但是我已经尝试过使用它并且失败了,如果需要,我还会继续创建一个交换方法。
任何见解都会受到欢迎,谢谢
//[][] of Objects
private MyObject[][] jaggedArray = new MyObject[3][]
{
new MyObject[5]
{
new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
},
new MyObject[5]
{
new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
},
new MyObject[5]
{
new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
},
};
static void swap(ref MyObject a, ref MyObject b)
{
MyObject temp = a;
a = b;
b = temp;
}
class MyObject
{
public MyObject()
{ Value = rand.Next(100); }
public int Value{ get; set; }
Random rand = new Random();
}
答案 0 :(得分:1)
以下代码在一个维度中排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
new Test();
}
}
public class Test
{
//[][] of Objects
private MyObject[][] jaggedArray = new MyObject[3][]
{
new MyObject[5]
{
new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
},
new MyObject[5]
{
new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
},
new MyObject[5]
{
new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
},
};
public Test()
{
jaggedArray = jaggedArray.Select(x => x.OrderBy(y => y).ToArray()).ToArray();
}
}
public class MyObject : IComparable<MyObject>
{
public MyObject()
{
Value = rand.Next(100);
}
public int Value{ get; set; }
static Random rand = new Random();
public int CompareTo(MyObject o)
{
return this.Value.CompareTo(o.Value);
}
}
}