If the title is not confusing enough maybe this will be. I have a linked list which contains people who have first and last names as well as a few other variables. The list must be sorted by last name first, then by first name. So far I insert the people into the list in alphabetical order by last name. I then try to traverse the list, if two last names are the same, I check the first names and swap. But I have a bug.
Inserted alphabetically into the list by last name, last,first
Acy,Mary
Acy,Clayton
Bob,Lonnie
Toni,Lonnie
After my so call "sort" of first names
Acy,Mary
Bob,Lonnie
Acy,Clayton
Toni,Lonnie
You can see it is sorted by last name. I am trying to sort each same last name by first name. And this is what I get as output from
public void sortFirstNames(){
System.out.println("here");
PeopleNode previous = null;
PeopleNode current = head;
PeopleNode temp;
if(head == null || head.next == null){
return;
}
while(current != null && current.next != null && current.lastName.compareTo(current.next.lastName) == 0){ //traverse the list
if((current.firstName).compareTo(current.next.firstName) > 0){ //see which first name goes first
temp = current.next.next;
current.next.next = temp.next;
temp.next = current.next;
current.next = temp;
current = temp.next;
}
current = current.next;
}
}
It doesn't change the list at all, I have taken advise of commenters but have yet to make it work. Does anyone have any idea?
Basically I am trying to say while two last names are the same check the first names, then swap them if need be.