This is another homework here. I need to sort students by it's last name and first name (firstly last name and secondly first name). The full list of students must be edited alphabetical.
What works so far:
If i type in 3 students with same Last name, it sorts correctly.
Lets say:
Richardson Mark Richardson Mike Richardson Matt
The correct sorting order is:
Richardson Mark Richardson Matt Richardson Mike
It also work when last name's start with same letter and are look alike
Lets say:
Richardson Mark Richmond Luke Rikkard Matt
Sorts as:
Richardson Mark Richmond Luke Rikkard Matt
My problem
The code doesn't sort 3 entirely different last name's (etc, Richardson, Markson, Bekhs)...
Please notice that only basic functions are allowed and must be programmed like bellow!
private static void sortAlpphabetical(Student[] studentList)
{
for (int i = 1; i < studentList.Length; i++)
{
for (int j = 0; j < studentList.Length - 1; j++)
{
string lastName1 = studentList[j].lastName.ToLower() + studentList[j].name.ToLower();
string lastName2 = studentList[j + 1].lastName.ToLower() + studentList[j + 1].name.ToLower();
for (int k = 0; k < lastName1.Length; k++)
{
if (returnIndex(lastName2[k]) > returnIndex(lastName1[k]))
{
Student currentStudent = studentList[j];
studentList[j] = studentList[j + 1];
studentList[j + 1] = currentStudent;
}
}
}
}
Console.WriteLine("List of students:\n");
for (int i = 0; i < studentList.Length; i++)
{
Console.WriteLine("//code");
}
}
When i try to sort 3 different last names, it gives me Index was outside the bounds of the array. Error
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in work.exe
Additional information: Index was outside the bounds of the array.