Question: C# Programming Platform II Sorting Basics Step 1: Make a console application named yourname_sortingbasics. Make sure that you use a windows classic desktop console application/.Net

C# Programming Platform II Sorting Basics

Step 1: Make a console application named yourname_sortingbasics. Make sure that you use a windows classic desktop console application/.Net Framework console application and not a .Net Core console application. Step 2: Make a Student class that has the following: a. StudentID, LastName, FirstName, CourseID and Course Grade properties. b. A constructor that collects the above property information. c. A method that retrieves test data for this Student class. This method will use the following code for testing purposes: public static List getTestStudents() { // For testing purposes some duplicate student infomation will be used. List students = new List

e. Support methods for adding a sort title and column headers for a report on your student data. These methods will be used to display student data before and after various sorting conditions. Step 3: In your main method test your Employee class with the following code: List students = Student.getTestStudents(); Console.WriteLine(Student.SortTitle("Not Sorted")); Console.WriteLine(Student.ColumnHeader()); foreach (Student student in students) { Console.WriteLine(student); } Console.WriteLine(" Press to quit..."); Console.ReadKey(); You should see a data dump on the test student data similar to the following:

Note: Google string format operations to see what options you have in terms of using right hand justification, left hand justification, PadLeft, PadRight, etc. Step 4: Modify your Student class to add a default last name, first name sort capability. The IComparable interface is used typically to compare the current class instance with other instances of the same type. To use this technique, you would add to the Student class the IComparable interface: public class Student : IComparable The compiler will then warn you that you need to implement the interface member CompareTo. Now implement the CompareTo method to allow sorting by last name and then first name. Note: when doing name comparisons you usually uppercase all of the string characters first before processing the sort options. Thus make sure you UpperCase first and last name so that no sort differences with occur based on case differences. Bill Gates versus bill Gates or bill gates.

10 points are scored if you correctly implement the CompareTo method to provide this last name, first name sort capability. To test your CompareTo method in your main method add the following code: // Sort using built in Student IComparer default sort... Console.WriteLine(Student.SortTitle("Sorted by Last Name and First Name Using Built in IComparable")); Console.WriteLine(Student.ColumnHeader()); students.Sort(); foreach (Student s in students) { Console.WriteLine(s); } Your console screen should then show the following:

Note: Original data is preserved. However, you can output upper cased string data if you wish. Step 5: Now examine the common situation of multiple sorts done on multiple sort fields. The IComparer interface is used when you want to compare 2 objects of the same type. It can be implemented with nested classes or separate external classes. As an example let's set up a separate class called StuSortCourseGradeLastFirst that provides sorting capability on course grade, then last name and first name: Points scored for implementing correctly a sort by course grade, last name and then first name: 10 points To test this sort add the following code to your Main method: Console.WriteLine(Student.SortTitle("Sorted by Course Grade, Last Name, and then First Name Using IComparer")); Console.WriteLine(Student.ColumnHeader()); students.Sort(new StuSortCourseGradeLastFirst()); foreach (Student s in students) { Console.WriteLine(s); } Step 6: Now sort your students by Last Name, First Name and then CourseID with another sort class such as StuSortLastFirstCourseID. Points: 10 Step 7: Finally let's sort your students by Last Name, First Name and then CourseID and then CourseGrade you would just add another sort class such as StuSortLastFirstCourseIDCourseGrade. Points: 10 we are not using LINQ, SQL or Lambda expressions yet for a sorting technique. When we examine functional programming, you are going to find that sorting becomes an extremely simple operation. Total points for this project: 50 (10 points for each sort condition for a total of 4 sorts) with the remainder of the points being distributed based upon project functionality, report formatting, coding style, etc.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!