With your methed you might be moving big segments of memory.
( You sort the data not the list).
I would use an array of pointers, with each points to a cell.
Than simply qsort this array. and build the list from the array.
Code:
void sort_list(STUDENT *stud, int n_elm)
{
STUDENT **ppstud;
int i, j;
if ( ! stud || ! stud->studentPtr )
return;
if ( ! ( ppstud = malloc(n_elm*sizeof(STUDENT*)) ) )
{
printf("error malloc\n");
return;
}
i = 0;
for ( stud ; stud ; stud->studentPtr )
ppstud[i++] = stud;
qsort(ppstud, i, sizeof(STUDENT*), comp_int);
for ( j = 0 ; j < i - 1 ; ++j )
ppstud[j]->studentPtr = ppstud[j+1];
ppstud[j]->studentPtr = NULL;
stud = ppstud[0];
free(ppstud);
return(stud);
}
int comp_int(const void *a, const void *b)
{
return( (*(STUDENT**)a)->ID, (*(STUDENT**)b)->ID );
}