How to use LINQ to sort a list with some conditions?

john zyd 421 Reputation points
2022-11-07T20:32:45.353+00:00
    public class Json_Students  
    {  
        public string CourseName { get; set; }  
        public string StudentID { get; set; }  
        public int FinalGrade { get; set; }  
        public int GradeFactor { get; set; }  
    }  
    private void Init_Students()  
    {  
        List<Json_Students> student_list = new();  
        Json_Students student1 = new()  
        {  
            CourseName = "Big",  
            StudentID = "Studen_01",  
            FinalGrade = 90,  
            GradeFactor = 9,  
        };  
        student_list.Add(student1);  
        Json_Students student2 = new()  
        {  
            CourseName = "Big",  
            StudentID = "Studen_02",  
            FinalGrade = 60,  
            GradeFactor = 5,  
        };  
        student_list.Add(student2);  
        Json_Students student3 = new()  
        {  
            CourseName = "Big",  
            StudentID = "Studen_03",  
            FinalGrade = 45,  
            GradeFactor = 3,  
        };  
        student_list.Add(student3);  
        Json_Students student4 = new()  
        {  
            CourseName = "Medium",  
            StudentID = "Studen_04",  
            FinalGrade = 90,  
            GradeFactor = 9,  
        };  
        student_list.Add(student4);  
        Json_Students student5 = new()  
        {  
            CourseName = "Medium",  
            StudentID = "Studen_05",  
            FinalGrade = 80,  
            GradeFactor = 8,  
        };  
        student_list.Add(student5);  
        Json_Students student6 = new()  
        {  
            CourseName = "Medium",  
            StudentID = "Studen_06",  
            FinalGrade = 100,  
            GradeFactor = 10,  
        };  
        student_list.Add(student6);  
        Json_Students student7 = new()  
        {  
            CourseName = "Small",  
            StudentID = "Studen_07",  
            FinalGrade = 90,  
            GradeFactor = 9,  
        };  
        student_list.Add(student7);  
        Json_Students student8 = new()  
        {  
            CourseName = "Small",  
            StudentID = "Studen_08",  
            FinalGrade = 100,  
            GradeFactor = 10,  
        };  
        student_list.Add(student8);  
    }  

Hello:
I have a few students records, I save them in one list: student_list.
I want to sort the list by the following conditions:

  1. The CourseName with the most number of students rank first. Therefore,
    CourseName with "Big" & "Medium" rank before ""Small". "Big" & "Medium" each has
    3 students, but "Small" has only 2 students.
  2. If more than one CourseName have the same number of students,
    then those CourseName with the most sum of FinalGrade multiplied by GradeFactor
    will rank first.
    For my above example:
    "Big" has the value = (90*9) + (60*5) + (45*3) = 1245
    "Medium" has the value = (90*9) + (80*8) + (100 * 10) = 2450
    "Medium" has higher vale than "Big", so "Medium" will rank before "Big"
  3. Loop through all the students according to the rules before, then
    show each studen't records, the student with the highest "FinalGrade"
    will be listed first, if 2 students has the same "FinalGrade", then
    the student with the higher "GradeFactor" will be listed first, if
    it is still a tie, then list the students with their alphabet name order,
    so "Student_01" will be listed before "Student_02".
    According to the above rules: the first one on the list is: "Student_06"
    the last one on the list is: "Student_07"
    Please advise,
    Thanks,
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,299 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,821 Reputation points
    2022-11-07T21:39:03.35+00:00

    Your data is not laid out such that this is directly possible. As soon as you start talking about combining records or making queries across records then you need an organizational structure in place. For your specific scenario you're going to need to group the students by the courses (to get the hierarchy) and then order the groups according to your course rules. Then you'll need to order the students within each course. Something like this might work.

       var groups = from s in students  
                    group s by s.CourseName into g               
                    select new {  
                               CourseName = g.Key,  
                               WeightedScore = g.Sum(x => x.FinalGrade * x.GradeFactor),  
                               TotalStudents = g.Count(),  
                               Students = g.ToArray()  
                           };  
         
         
       var ordered = groups.OrderByDescending(x => x.TotalStudents).ThenByDescending(x => x.WeightedScore);  
       foreach (var group in ordered)  
           foreach (var student in group.Students.OrderByDescending(s => s.FinalGrade).ThenByDescending(s => s.GradeFactor).ThenBy(s => s.StudentID))  
               Console.WriteLine(student.StudentID);  
    

    But if you reorganized your data such that courses contained students then you could simplify the grouping and just be left with the ordering.

    0 comments No comments