Hi everyone...I am building a program to input students names(first and last) and also GPA. I have everything working but the sort by last name. Everything works fine, even the GPA sort but the last name sort will not. Please help. Below is the function in my program i am trying to use to do the sort. I am using case 1 of the switch statement to do the GPA and case 2 the last name.
void Sort(int length, struct student allstud[], int func)
{
int i, j, flag = 1; // set flag to 1 to start first pass
struct student temp; // holding variable
for(i = 1; (i < length) && flag; i++)
{
flag = 0;
for (j=0; j < (length-1); j++)
{
switch(func)
{
case 1:
if (atof(allstud[j+1].GPA) > atof(allstud[j].GPA)) // ascending order simply changes to <
{
temp = allstud[j+1];
allstud[j+1]=allstud[j];
allstud[j]=temp;
flag = 1; // indicates that a swap occurred.
}
break;
case 2:
{
if (allstud[j+1].Lastname[0] > allstud[j].Lastname[0]) // ascending order simply changes to <
{
temp = allstud[j+1];
allstud[j+1]=allstud[j];
allstud[j]=temp;
flag = 1; // indicates that a swap occurred.
}
break;
}
}
}
}