I have a calculation that sorts books by rating...
System.out.println("\r" + "\n\tSorted by Rating\n"); for (int count = 0; count < bookL.size(); count++) { for (int in = 0; in < bookList.size() - 1; in++) if (videoList.get(in).getRating() < bookList.get(in + 1).getRating()) { Book temp = bookList.get(in); bookList.set(in, bookList.get(in+1)); bookList.set(in+1, temp); } System.out.println(bookList.get(count).getRating() + " " + bookList.get(count).getTitle()); }
I was told that I could easily alter the code to sort alphabetically as well.
for (int count = 0; count < bookList.size(); count++) { for (int in = 0; in < bookList.size() - 1; in++) if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) { Book temp = bookList.get(in); bookList.set(in, bookList.get(in+1)); bookList.set(in+1, temp); } System.out.println(bookList.get(count).getRating() + " " + bookList.get(count).getTitle()+"\t"); }
What am I missing here? Why doesn't this work?