I'm self-learning C# but I'm still new to it. I want to figure out how to sort multiple objects in List. Here's a tiny snippet of my code. Here's a clarification, I want to order the list in place (not creating a new one) by either midterm/studentName or Final exam score while displaying all three values in the list.
There are two classes. I'm trying to sort the items by students, then midterm scores and then final exam scores individually. as displayed in the toString method of my StudentGrades class. I've looked at the delegate method but I still don't understand how to implement it from https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort?view=netcore-3.1 .
Any advice on a simpler approach would help.
public class StudentGrades
{
public string studentName;
public int MidtermGrade;
public int finalExamGrade;
public StudentGrades()
{
//initialize variables to 0 or ""
}
//some methods here
public string toString()
{
string result = "student: " + studentName + " midterm grade: " + midterm + ...etc;
return result;
}
}
public partial class Grades : Form1
{
List<StudentGrades> studentList;
//StudentGrades is a separate class with no parameters in the constructor
public Grades(List<StudentGrades> student)
{
this.studentList = student;
// some code
}
private void button3_click(Object sender, EventArgs e)
{
this.studentList.Sort(); //default sorting method for sorting students
}
private void button4_click(//same arguments)
{
//sort midterm grades, but don't know how to do it
}
}