0

I have Student class and the initialized list of students to the Program class in my project. Now I want to get the highest score of each student, in order

How to get that using Max()?

public class Student
{
    public string First { get; set; }

    public string Last { get; set; }

    public int ID { get; set; }

    public List<int> Scores;
} 

// Create a data source by using a collection initializer.

static List<Student> students = new List<Student>
{
   new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
   new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
   new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
   new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
   new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
   new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
   new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
   new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
   new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
   new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
   new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
   new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
};
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
Tram Pham
  • 43
  • 1
  • 4
  • Can you show what you've tried so far? If you haven't tried anything: start with `students.Select` – ProgrammingLlama Jun 01 '19 at 03:02
  • 1
    Possible duplicate of [How to use LINQ to select object with minimum or maximum property value](https://stackoverflow.com/questions/914109/how-to-use-linq-to-select-object-with-minimum-or-maximum-property-value) – Fabio Jun 01 '19 at 03:08

2 Answers2

1

Something like this:

var studScores=students.Select(x=>new 
            {
                student=x,
                highestscore=x.Scores.DefaultIfEmpty(0).Max()
            }).OrderByDescending(m=>m.highestscore);

            foreach(var score in studScores)
            {
                Console.WriteLine(score.highestscore+" "+score.student.First);
            }

Output

99 Fadi
99 Terry
97 Svetlana
97 Cesar
96 Eugene
94 Sven
94 Hichael
93 Hanying
92 Hugo
92 Lance
91 Claire
91 Debra
bolkay
  • 1,881
  • 9
  • 20
  • And `x.Scores.DefaultIfEmpty(0).Max()` would be better since you can get `System.InvalidOperationException: Sequence contains no elements` if you call `.Max()` on an empty collection. – ProgrammingLlama Jun 01 '19 at 05:47
-2

Traversing students and then using the bubble sorting algorithm to maximize

    static void Main(string[] args)
    {
        List<Student> students = new List<Student>();
        students.Add(new Student { First = "Svetlana", Last = "Omelchenko", ID = 111, Scores = new List<int> { 97, 92, 81, 60 } });
        students.Add(new Student { First = "Svetlana", Last = "Omelchenko", ID = 111, Scores = new List<int> { 75, 84, 91, 39 } });

        for (int i = 0; i < students.Count; i++)
        {
            Student item = students[i];
            List<int> scores = item.Scores;
            int max = SortGetMax(scores);
            Debug.WriteLine(max);
        }
    }

    public static int SortGetMax(List<int> arrays)
    {
        int max = 0;
        for (int i = 0; i < arrays.Count; i++)
        {
            if (arrays[i] > max)
            {
                int flag = arrays[i];
                arrays[i] = max;
                max = flag;
            }
        }
        return max;
    }
verejava
  • 7
  • 5
  • Why does `SortGetMax` mutate the input, and also not output the max? What is it supposed to do? If I call `List test = new List() { 5, 3, 8, 1, 2, 4, 3, 9 }; var max = SortGetMax(test);`, the output is `8` when clearly it should be `9`. The values in `test` become `0, 3, 5, 1, 2, 4, 3, 9` after executing your method, and after executing it a second time they become `0, 0, 3, 1, 2, 4, 3, 9`. – ProgrammingLlama Jun 01 '19 at 04:24
  • some mistake need change for (int i = 0; i < arrays.Count - 1; i++) to for (int i = 0; i < arrays.Count; i++) – verejava Jun 01 '19 at 05:36
  • That still leaves the issue of it mutating the original list in a bad way. – ProgrammingLlama Jun 01 '19 at 05:42
  • dont' reinvent the wheel. .Net has methods for sorting things, tested by .Net team and successfully used by thousands of developers. No need to write your own sorting algorithm, test it and fix bugs. – Gian Paolo Jun 01 '19 at 07:44