1. ## sorting question

I have a linked list I am trying to sort... I wrote this function to sort it.... I go through it logically and I don't see why it wouldn't work.... does anyone know a better, simpler way to sort a linked list to change it to an ascending order?

this isn't working for me, but I dont know why:

Code:
```void sort_list(STUDENT* stud) {

STUDENT* first;
STUDENT* second;
STUDENT temp_student;

first = stud;

while( first != NULL ) {

second = first -> studentPtr ;

while( second != NULL ) {

if ( (first -> ID) < (second -> ID) ) {

temp_student.ID = first->ID;
strcpy(temp_student.name,first->name);
first->ID = second->ID;
second->ID=temp_student.ID;
strcpy(second->name,temp_student.name);

strcpy(temp_student.name,"");
temp_student.ID = 0;

}
second = second -> studentPtr;
}
first = first -> studentPtr;
}
}```

2. ## I would like all opinion

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  );
}```

3. Code:
`for ( stud ; stud ; stud->studentPtr )`
This does nothing except loop forever if stud isn't NULL. It's the same as writing:
Code:
`for ( ; stud ; )`

4. ## You are dead wrong

This is a linked list !!!

5. No, I am definitely not dead wrong. It has nothing to do with the fact that it's a linked list you're "attempting" to walk through. The value of stud never changes throughout your loop so every element in your ppstud array will get set to the same value. Since stud never changes you'll soon overrun the bounds of your ppstud array and start stomping all over memory you shouldn't until the program crashes with a segmentation fault or whatever your OS reports when you touch memory you shouldn't. At any rate, it's not going to do what you want.

Go ahead and try it if you don't believe me. You need to read up on for() loops.

6. ## Sorry U are right.

Sorry again missed it. That is wrong :

for ( stud ; stud ; stud->studentPtr )

correct :
for ( stud; stud ; stud = stud->studentPtr )

Again Very sorry.