public int Compare(Student student1, Student student2) { if (student1.Grade.CompareTo(student2.Grade) != 0) { return(student1.Grade.CompareTo(student2.Grade)); } else { StudentCompareByName compareByName = new StudentCompareByName(); return(compareByName.Compare(student1, student2)); //if grades are the same, then sort by name (first name, last name, then ID) } }
protected void sortType_SelectedIndexChanged(object sender, EventArgs e) { List <Student> courseList = new List <Student>(); courseList = (List <Student>)Session["courseListSession"]; if (sortType.SelectedItem.Value == "0") { //ID was selected for sorting courseList.Sort(); foreach (Student st in courseList) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = st.ID.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = st.Name.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = st.Grade.ToString(); row.Cells.Add(cell); tableSort.Rows.Add(row); } } if (sortType.SelectedItem.Value == "1") { //Name was selected for sorting StudentCompareByName myComp = new StudentCompareByName(); courseList.Sort(myComp); foreach (Student st in courseList) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = st.ID.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = st.Name.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = st.Grade.ToString(); row.Cells.Add(cell); tableSort.Rows.Add(row); } } if (sortType.SelectedItem.Value == "2") { //Grade was selected for sorting StudentComparerByGrade myComp = new StudentComparerByGrade(); courseList.Sort(myComp); foreach (Student st in courseList) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = st.ID.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = st.Name.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = st.Grade.ToString(); row.Cells.Add(cell); tableSort.Rows.Add(row); } } }
protected void addBtn_Click(object sender, EventArgs e) { //creating a new student string studentNumber = studentNumberTxt.Text; string studentName = studentNameTxt.Text; AlgonquinCollege.Registration.Entities.Student newStudent = new AlgonquinCollege.Registration.Entities.Student(studentNumber, studentName); //create a new course string index = (string)Session["selectedCourse"]; string selectedCourseName = DropDownList1.SelectedValue.ToString(); Course newCourse = new Course(index, selectedCourseName); //creating a list of academic records List <AcademicRecord> academicRecordList = new List <AcademicRecord>(); AcademicRecord newAcademicRecord = new AcademicRecord(newCourse, newStudent); newAcademicRecord.Grade = int.Parse(gradeTxt.Text); //adding course to list and session if (Session["academicRecordListSession"] == null) { academicRecordList.Add(newAcademicRecord); Session["academicRecordListSession"] = academicRecordList; } else { academicRecordList = (List <AcademicRecord>)Session["academicRecordListSession"]; academicRecordList.Add(newAcademicRecord); Session["academicRecordListSession"] = academicRecordList; } //sorting list by name StudentCompareByName myComp = new StudentCompareByName(); academicRecordList.Sort(myComp); //displaying table foreach (AcademicRecord item in academicRecordList) { if (item.Course.CourseName == DropDownList1.Text) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = item.Student.Id.ToString(); row.Cells.Add(cell); cell = new TableCell(); cell.Text = item.Student.Name; row.Cells.Add(cell); cell = new TableCell(); cell.Text = item.Grade.ToString(); row.Cells.Add(cell); tableSort.Rows.Add(row); } } studentNumberTxt.Text = ""; studentNameTxt.Text = ""; gradeTxt.Text = ""; }