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

时间:2013-10-03 22:09:56

标签: c# list sorting highest

有两个清单。

我有一份学生名单,就像这样

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

列表中每个学生对象的属性是firstName,LastName和Fees []。

费用是一个数组,为列表中的每个学生提供一系列费用。

我做了第二个这样的清单:

List<double>  totals = new List<double>();

我遍历学生名单并加上每项学费。然后我将每个学生的总数添加到totals列表(我的第二个列表)。

现在我需要对students列表进行排序,以便收取最高的费用总额。换句话说,我需要使用students中的最高值对totals进行排序。我可以像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????

请帮忙。提前致谢。

4 个答案:

答案 0 :(得分:3)

如果我做得对:

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

这将使最高费用金额的学生处于最高位置,另一位将保持无序状态。

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

double highestTotalAmountDue = totals.Max();
int highestIndex = totals.IndexOf(highestTotalAmountDue );
var student = student[highestIndex];
students.RemoveAt(highestIndex);
students.Insert(highestIndex,studet);

答案 3 :(得分:0)

我认为关键代码是......

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