How do I sort two array's of of structs(one of the arrays is a member of the other struct) with the same function using bubble sort (Descending order for students[] on Student::name and sort the classes[] array on Class::title?
struct Class
{
string title;
int units;
char grade;
};
struct Student
{
string name;
double gpa;
Class classes[500];
};
In main:
Student students[SIZE];
I am attempting to sort an array of structs that each contain an array of structs that also need to be sorted using bubble sort. My sorting function is pasted below. It does not sort correctly rather it sorts the inner array of structs classes[] according to title correctly, and sorts the outer array st[] correctly on the first iteration of the for loop. Since on the second iteration the elements of st[] have been swapped the first element doesn't get sorted b/c currentStu is now set to the second element in the array.
void sort_name(Student st[], int numValues)
{
int currentStu = 0;
int currentClass = 0;
for(currentStu = 0; currentStu < numValues; currentStu++)
{
for(currentClass = 0; st[currentStu].classes[currentClass].title != ""; currentClass++)
{
bubbleUpClass(st, currentClass, currentStu);
}
bubbleUpLastName(st, currentStu, numValues - 1);
}
}
studentsshould home after your first pass; in slots[0..1] after two passes, etc. is your bubble function pushing items to the end of the table? if so, it may be backwards. It is also possible your invoke should bebubbleUpLastName(st, currentStu, numValues - currentStu);