[英]Using the highest value from one list to sort the second list.
There are two lists. 有两个列表。
I have a list of students, made like this 我有这样的学生名单
List<Student> students = new List<Student>();
The properties of each student object in the list are firstName, LastName, and Fees[]. 列表中每个学生对象的属性是firstName,LastName和Fees []。
Fees is an array that holds a set of fees for each student in the list. 费用是一个数组,其中包含列表中每个学生的一组费用。
I made a second list like this: 我列出了第二个这样的列表:
List<double> totals = new List<double>();
I loop through the list of students and add up the fees for each. 我逐一浏览学生名单,并为每个学生加算费用。 Then I add the totals for each student to the totals
list (My second list). 然后,我将每个学生的totals
添加到totals
列表(我的第二个列表)中。
Now I need to sort the students
list so the highest total amount due in fees is at the beginning. 现在,我需要对students
列表进行排序,以便在开始时支付最高的费用总额。 In other words, I need to sort students
using the highest value in totals
. 换句话说,我需要使用totals
最高的值对students
进行排序。 I can get the highest value out of totals
like this: 我可以从totals
获得最高价值,如下所示:
double highestTotalAmountDue = totals.Max();
How do I use this highestTotalAmountDue
value to sort the students
list so that the student with the highest fee total is at the beginning of the list?? 我如何使用此highestTotalAmountDue
值对students
列表进行排序,以使总费用最高的学生位于列表的开头?
Just to clarify, I only need to add the student with the highest fee total at the top of the list. 为了澄清,我只需要在列表的顶部添加费用最高的学生。 The rest can remain in the same order. 其余的可以保持相同的顺序。
Here is my code so far: 到目前为止,这是我的代码:
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????
Please help. 请帮忙。 Thanks in advance. 提前致谢。
If i've got it right: 如果我做对了:
var orderedStudents = students
.OrderByDescending(s => s.Fees.Sum(f => f.Amount) == highestTotalAmountDue);
This will put the max-fee-amount student(s) at the top, the other will remain unordered. 这会将最大费用学生放在顶部,其他学生则保持无序状态。
Do away with the list of totals. 取消总计列表。 You can track the highest total as follows, then re-insert the student at the top of the list. 您可以按照以下方式跟踪最高总数,然后将学生重新插入列表顶部。
List<Student> students = new List<Student>();
// populate the students list
// Mark the student with the highest total as we find him.
Student highestTotalStudent = null;
var highestTotal = 0.0;
foreach (var student in students)
{
var tempTotal = 0.0;
for (var i = 0; i < resultSet[0].Fees.Length; i++)
{
tempTotal += student.Fees[i].Amount;
}
if (tempTotal > highestTotal)
{
// We have a new winner
highestTotal = tempTotal;
highestTotalStudent = student;
}
}
// Finally, remove the located student, and re-insert her at the top of the list
students.Remove(highestTotalStudent);
students.Insert(0, highestTotalStudent);
double highestTotalAmountDue = totals.Max();
int highestIndex = totals.IndexOf(highestTotalAmountDue );
var student = student[highestIndex];
students.RemoveAt(highestIndex);
students.Insert(highestIndex,studet);
I think the key code is... 我认为关键代码是...
var studentToMove = students.FirstOrDefault(x => x.FeesTotal == students.Max(s => s.FeesTotal));
students.Remove(studentToMove);
students.Insert(0, studentToMove);
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student()
{
FirstName = "Joe", LastName = "Smith",
Fees = new Fee[]
{
new Fee()
{
Amount = 55
},
new Fee()
{
Amount = 100
}
}
},
new Student()
{
FirstName = "Jane", LastName = "Smith",
Fees = new Fee[]
{
new Fee()
{
Amount = 400
},
new Fee()
{
Amount = 32
}
}
},
new Student()
{
FirstName = "Sam", LastName = "Smith",
Fees = new Fee[]
{
new Fee()
{
Amount = 3
},
new Fee()
{
Amount = 10
}
}
}
};
var studentToMove = students.FirstOrDefault(x => x.FeesTotal == students.Max(s => s.FeesTotal));
students.Remove(studentToMove);
students.Insert(0, studentToMove);
foreach (var student in students)
{
Console.WriteLine("Student: " + student.FirstName + " " + student.LastName);
}
}
}
class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Fee[] Fees { get; set; }
public decimal FeesTotal
{
get
{
if (Fees == null || Fees.Length == 0)
return 0;
return Fees.Sum(x => x.Amount);
}
}
}
class Fee
{
public decimal Amount { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.