【发布时间】:2013-10-10 19:01:03
【问题描述】:
有两个列表。
我有一个学生名单,是这样制作的
List<Student> students = new List<Student>();
列表中每个学生对象的属性是 firstName、LastName 和 Fees[]。
Fees 是一个数组,其中包含列表中每个学生的一组费用。
我做了第二个这样的列表:
List<double> totals = new List<double>();
我遍历学生列表,并将每个学生的费用相加。然后我将每个学生的总数添加到totals 列表(我的第二个列表)。
现在我需要对students 列表进行排序,因此最高的应付费用总额位于开头。换句话说,我需要使用totals 中的最高值对students 进行排序。我可以像这样从totals 中获得最高值:
double highestTotalAmountDue = totals.Max();
如何使用此highestTotalAmountDue 值对students 列表进行排序,以使总费用最高的学生位于列表的开头??
为了澄清,我只需要将总费用最高的学生添加到列表顶部。其余的可以保持相同的顺序。
到目前为止,这是我的代码:
List<double> totals = new List<double>();
double tempTotal = 0;
Lis<Student> students = new Lis<Student>();
// populate the students list
foreach (var item in students)
{
for (var i = 0; i < resultSet[0].Fees.Length; i++)
{
tempTotal += item.Fees[i].Amount;
}
totals.Add(tempTotal);
tempTotal = 0;
}
double highestTotalAmountDue = totals.Max();
// now what to do to sort the students list by highestTotalAmountDue to put the student with the highest fee due at the top????
请帮忙。提前致谢。
【问题讨论】: