简体   繁体   English

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

[英]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.

相关问题 C#在列表中对多个数字进行排序。 按第二个字符串排序 - C# sorting multiple numbers in a list. Sort by second string 从属下拉列表。 从一个列表过滤到另一个 - Dependent Dropdown list. Filtering from one list to another 如何从c#中的列表中选择第二高的值? - How can I choose the second highest value from a list in c#? 从列表中删除列表。 数字不匹配 - Removing list from list. Numbers mirmatch 根据最高价值从列表中获取前5名 - Grab top 5 from a list based on highest value 从列表中删除与其他List中任何其他Object具有相同值的所有对象。 XNA - Remove all the objects from a list that have the same value as any other Object from an other List. XNA 通过另一个列表中的元素过滤列表。 错误:无法创建类型为&#39;xyz&#39;的常量值 - Filter a list by the elements from another list. Error: Unable to create a constant value of type 'xyz' 从列表中选择一个随机变量。 如果不是所选的,则选择另一个。 - Pick a random random from a list. If it's not the chosen one, pick another one. 使用LINQ从另一个属性分组的列表中删除除最大值以外的所有值 - Remove all but the highest value from a List grouped by another property using LINQ 使用另一个列表的属性过滤对象列表。 使用linq - Filter a list of objects using a property that is another list. Using linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM