I have a LinkedList full of Book objects which are stored in Nodes, which contain the year of publication and the title of the book. I am trying to sort the list so that it is arranged from oldest year to most recent year, however, after the first Book, the rest of my books now have the same title and year of publication. I believe it has something to do with when I make the swap using my setBook method.
public void sortByYear(){
Node current = head;
Node next = null;
if(isEmpty()){ //if the head is null
return;
}
while(current != null){
next = current.getNext();
while(next != null){
if(current.getBook().getYear() > next.getBook().getYear()){
Book temp = current.getBook();
current.setBook(next.getBook().getYear(), next.getBook().getTitle());
next.setBook(temp.getYear(), temp.getTitle());
// current.getBook() = next;
// next.getBook() = temp;
}
next = next.getNext();
}
current = current.getNext();
}
}