使用一个列表中的最大值对第二个列表进行排序

本文关键字:列表 最大值 第二个 排序 一个 | 更新日期: 2023-09-27 18:33:54

有两个列表。

我有一份学生名单,是这样的

List<Student> students = new List<Student>();

列表中每个学生对象的属性是名字、姓氏和费用[]。

费用

是一个数组,其中包含列表中每个学生的一组费用。

我做了第二个这样的列表:

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????

请帮忙。提前谢谢。

使用一个列表中的最大值对第二个列表进行排序

如果我

做对了:

var orderedStudents = students
    .OrderByDescending(s => s.Fees.Sum(f => f.Amount) == highestTotalAmountDue);

这将使最高费用金额的学生位于顶部,另一个将保持无序。

取消总计列表。 您可以按如下方式跟踪最高总数,然后在列表顶部重新插入学生。

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);

我认为关键代码是...

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; }
}