# Thread: help with sorting a list :(

1. you mean something like this?

Code:
```void swap(charNode **a, charNode **b, charNode *start) {
charNode *temp, *preTemp;
charNode *preA = pred(start->next, start);
charNode *preB = pred(start, start);

preTemp = preA;

temp = *a;
*a = *b;
*preB = *preA;
*b = temp;
preB = preTemp;

}```

*CRASH*

this is killing me :|

2. No, like this :

Code:
```charNode* temp2 = (*preA).next;
(*preA).next = (*preB).next;
(*preB).next = temp2;```

3. Code:
```void swap(charNode **a, charNode **b, charNode *start) {
charNode *temp, *preTemp;
charNode *preA = pred(start->next, start);
charNode *preB = pred(start, start);
charNode* temp2 = (*preA).next;

temp = *a;
*a = *b;
*b = temp;

(*preA).next = (*preB).next;
(*preB).next = temp2;

}```
*CRASH*

4. Oh for chrissakes.

Code:
```void swap(charNode *a, charNode *b)
{
char temp;
size_t i;

for (i = 0; i < 12; i++)
{
temp = a->petName[i];
a->petName[i] = b->petName[i];
b->petName[i] = temp;
}
}```

5. zx-1, i was just about to post the progress i made. The only problem was that the solution could only swap start and next node. Your solution however if i input any node it swaps it..hmm

i.e.:

Code:
```void *swap(charNode **start, charNode *a, charNode *b)
{

charNode *d = a->next->next->next;

a->next = b->next;
b->next->next = b;
b->next= d;
}```

6. actually your solution doesnt swap the nodes at all but just the contents of the struct which is cheating? you also don't need that for loop. it works fine without it.

7. so i'm guessing its not possible to have a function like the one you have there that swaps the nodes and reconstructs the list? is it too complex? because i can swap the nodes if i know where they are but as soon as i try it on the middle node and the next for example it stuffs up the list.

i.e.:

works fine for swapping start and the next node.

Code:
```	charNode *d = a->next->next;
a = b;
b->next->next=d;```
this works fine for swapping the node after next and the one after next,next.

Code:
```charNode *d = a->next->next->next;

a->next = b->next;
b->next->next = b;
b->next= d;```
but i cant seem to make a unified solution that does swapping regardless of the possition ?!?

8. Also, you have to swap the next pointer of each of the nodes you're swapping. I don't know why I didn't think of that. But that's important too. Also, you could very well just use zx-1's solution. It'll get the job done.

9. yea agreed... it's better to actually swap the physical string than to modify the pointer

10. I've always preferred to swap the the pointers around, as it just seems logical for me. But moving data accross nodes would work the same.

Also, it's easy right now to just copy over an array, but when your structs start containing a little more data, you'll be spending a lot of time copying everything over...