I have created a class named Student and I have the following code inside Program.cs:

public static IList<Student> Students { get; private set; }

private static void AddStudent()
    {
        /*try
        {
            Console.Write("First name: ");
            //Students.FirstName = Console.ReadLine();
            string firstName = Console.ReadLine(); 
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("Name was left empty.");
        }*/

        Console.Write("First name: ");
        string firstName = Console.ReadLine();

        Console.Write("Last name: ");
        string lastName = Console.ReadLine();            

        var newStudent = new Student {FirstName = firstName, LastName = lastName}; //if I use try-catch block, it says: The name 'firstName' doesn't exist in the current context

        Students.Add(newStudent);

        Console.WriteLine("The new student is added. \nEnter any key to return to main screen.");
        Console.ReadKey();
}


public static void SortStudents(IList<Student> students)
    {
        string temp;
        for (int i = 0; i < students.Count; i++)
        {
            for (int j = 0; j < students.Count; j++)
            {
                if (string.Compare(students[i].ToString(), students[j].ToString()) < 0)
                {
                    //swap
                    temp = students[i].ToString();
                    students[i] = students[j];
                    students[j] = temp; //error here
                }
            }
        }
        Console.WriteLine(students);
    }

Student class:

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string StudentNumber { get; set; }
    public string Gender { get; set; }
    public string FieldOfStudy { get; set; }
}

I am trying to implement an algorithm that will sort the entered names alphabetically, but it throws the error there. How can I fix that? I am also trying to use the try-catch block but it throws the error I have commented inside the code. Thanks in advance!

2 Answers

1
Josie G. Bigler On Best Solutions

As some of the commentators have pointed out you will likely need to access the properties of Student and compare them, and then swap the Objects.

So something like this:

public static void SortStudents(IList<Student> students)
    {
        //We change this to Type Student, not string.
        Student temp;
        for (int i = 0; i < students.Count; i++)
        {
            for (int j = 0; j < students.Count; j++)
            {
                //We look at the Properties of the object, not the Object.ToString()  
                if (string.Compare(students[i].FirstName, students[j].FirstName) < 0)
                {
                    //Here we are swapping the objects, because we have determined 
                    //Their first names aren't in alphabetical order.  
                    temp = students[i];
                    students[i] = students[j];
                    students[j] = temp;
                }
            }
        }
        //For loop, or Foreach loop here to iterate through your collection (ILIST)
    }
0
JohnG On

Is there some reason the Student class can not implement this “sorting” logic by implementing the IComparable interface? Using a List<Student> to hold the Student objects will use this CompareTo method to “sort” the objects. This will allow the class to be “sorted” anyway you want. In this case it is sorted by last name then first name. Have you tried to do this? It may look something like below…

public class Student : IComparable {
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public int Age { get; set; }
  public string StudentNumber { get; set; }
  public string Gender { get; set; }
  public string FieldOfStudy { get; set; }

  public int CompareTo(object obj) {
    Student that = (Student)obj;
    if (this.LastName.Equals(that.LastName))
      return this.FirstName.CompareTo(that.FirstName);
    return this.LastName.CompareTo(that.LastName);
  }
}

Then, to “sort” a List<Student> would simply be…

Students.Sort();