C# 对交错的对象数组进行排序
C# Sorting a jagged array of objects
使用 C#,我有一个包含对象的 2D Jagged 数组,我想要实现的是根据这些对象中的 public 属性 对这个 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();
}
下面的代码在一维中排序
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);
}
}
}
使用 C#,我有一个包含对象的 2D Jagged 数组,我想要实现的是根据这些对象中的 public 属性 对这个 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();
}
下面的代码在一维中排序
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);
}
}
}