Question

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

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

Now I need to sort the students list so the highest total amount due in fees is at the beginning. In other words, I need to sort students using the highest value in totals. I can get the highest value out of totals like this:

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

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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.

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; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top