Here’s the method
public void sortStudentsAlphabeticallyByFirstName()
{
StudentNode unsorted = tail;
StudentNode current = header;
while(current != null)
{
while(current != unsorted)
{
int result = (current.nextNode().getFirstName()).compareToIgnoreCase(current.getFirstName());
if(result < 0)
{
StudentNode temp = current;
current = current.nextNode();
current.setNext(temp);
}
}
current = current.nextNode();
unsorted = unsorted.prevNode();
}
}
The problem is that when executed it just keeps running and doesn’t stop and I’m not sure where the problem is.
Consider our Link List has A, C, B and D nodes. say as you enter in your second while loop
so using this code :
So seems like you forgot these steps,
When you are moving current to the next node after that, temp and current will swap. But the one previous to temp i.e. A in the example must point to B, which is being swapped with C. Since B was pointing to D before, now after swapping C must point to D (this part you missed) and B must point to C (that’s what you did on the third line.)
EDIT
Whole working code has been added for more information.
Hopefully that might help.
Regards