1

I have home object : id , FirstName, Lastname, Fees[ ]

Fees is object contain Amount description DueDate ...

home object include the Fees array

I have list of homes and every home has array/list from fees

I need to sort the fees depend on the Duedate in each object

for (int i = 0; i < homes.Length; i++)
  {
   homes[i].Fees = homes[i].Fees.OrderBy(si => si.DueDate).ToArray();
  }

Can I find one statement instead of four statements?


Can I find one statement instead of three statements?

That should rarely be your goal.

However; you say that Fees is an array; if that is the case, you can avoid some work by sorting in-place rather than creating a clone of the array:

foreach(var home in homes) {
   Array.Sort(home.Fees, (x,y) => x.DueDate.CompareTo(y.DueDate));
}
4

1 回答 1

5

我可以找到一个语句而不是三个语句吗?

这应该很少是你的目标。

然而; 你说那Fees是一个数组;如果是这种情况,您可以通过就地排序而不是创建数组的克隆来避免一些工作:

foreach(var home in homes) {
   Array.Sort(home.Fees, (x,y) => x.DueDate.CompareTo(y.DueDate));
}
于 2013-02-13T20:22:30.240 回答