Hi I'm having problems with java and the collections.sort method. I can't figure out how to sort an arraylist using the compareTo and Comparable interface. I've created the compareTo method and so on, but how do I sort an arraylist in another class.
I have created a book class and a CollectionOfBooks class, and implemented the Comparable in Book-class. But how do I sort the CollectionOfBooks arraylist?
When I try to sort it, it gives me the error "The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments"
From what I understand, the Collection.sort uses a List and then takes as arguments the list that is to be sorted and then what comparable-method to be used?
I've added the code below to clarify what i mean =)
package se.kth.labb3b; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.Serializable; import java.util.List; import java.util.Scanner; public class UserInterface implements Serializable { public static void main(String[] args) { UI ui = new UI(); int running = 1; //note the use of abstract base class references try{ //use buffering InputStream file = new FileInputStream( "books.dat" ); InputStream buffer = new BufferedInputStream( file ); ObjectInput input = new ObjectInputStream ( buffer ); try{ //deserialize the List List<Book> recoveredBooks = (List<Book>)input.readObject(); //display its data for(Book book: recoveredBooks){ System.out.println("Recovered books: " + book); ui.readBook(book); } } finally{ input.close(); } } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); System.out.println("File books.dat wasn't found!"); } while (running == 1) { System.out.println("\nWelcome to Lars and Daniels bookrecord!\n\n" + "1) Add a new book\n" + "2) List all objects\n" + "3) Search\n" + "4) Write to file"); Scanner scan = new Scanner(System.in); int answer = scan.nextInt(); switch (answer) { case 1: ui.addBook(); break; case 2: ui.getAllBooks(); break; case 3: System.out.print("Search title: "); //ui.getBooksByTitle(scan.nextLine()); break; case 4: ui.writeToFile(); break; default: System.out.println("Fail"); running = 0; break; } } } }
package se.kth.labb3b; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class UI implements Serializable { Book books; CollectionOfBooks cob = new CollectionOfBooks(); public void addBook() { System.out.print("Book name: "); Scanner scan = new Scanner(System.in); String bookName = scan.nextLine(); System.out.print("ISBN: "); int isbn = scan.nextInt(); System.out.print("Edition: "); int edition = scan.nextInt(); System.out.print("Price: "); double price = scan.nextDouble(); ArrayList<Author> lista = new ArrayList<Author>(); System.out.print("Numbers of authors: "); int numberOfAuthors = scan.nextInt(); for (int i = 0; i <= numberOfAuthors; i++) { System.out.println("Author: "); lista.add(new Author(scan.nextLine())); } books = new Book(bookName, isbn, edition, price, lista); cob.addBook(books); } public void readBook(Book book){ cob.addBook(book); } /*public void getBooksByTitle(String bookname) { List<Book> temp = cob.remoteSort(); Collections.sort(temp); System.out.println("Book:"); String tempbook = Collections.binarySearch(temp, bookname); System.out.println(tempbook); }*/ public void getAllBooks() { System.out.println("Book titles:"); for (int j = 0; j < cob.getNumberOfBooks(); j++) { System.out.print(cob.toString(j)); } } public void writeToFile(){ List<Book> temp = cob.remoteSort(); try{ //use buffering OutputStream file = new FileOutputStream( "books.dat" ); OutputStream buffer = new BufferedOutputStream( file ); ObjectOutput output = new ObjectOutputStream( buffer ); try{ output.writeObject(temp); } finally{ output.close(); } } catch(IOException e){ e.printStackTrace(); } } }
package se.kth.labb3b; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionOfBooks { private ArrayList<Book> Books = new ArrayList<Book>(); public void addBook(Book book) { Books.add(book); } public void removeBook(Book book) { Books.remove(book); } public int getNumberOfBooks() { return Books.size(); } public ArrayList<Book> getBooksByTitle(String title) { return Books; } public ArrayList<Book> remoteSort(){ return Books; } public String toString(int i){ String back; back = ("Title: " + Books.get(i).getTitle()); back = back + "\nISBN: " + Integer.toString(Books.get(i).getIsbn()); back = back + "\nRevision: " + Integer.toString(Books.get(i).getEdition()); back = back + "\nPrice: " + Double.toString(Books.get(i).getPrice()); back = back + "\nAuthor: \n"; ArrayList<Author> author = Books.get(i).getAuthors(); for(int j = 0; j < author.size(); j++){ back = back + "\t" + author.get(j).getAuthor() + "\n"; } System.out.println("*****************************************************'"); return back; } }
package se.kth.labb3b; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; public class Book implements Comparable<Book>, Serializable { private String title; private int edition, isbn; private double price; private ArrayList<Author> Author = new ArrayList<Author>(); public Book(String title, int isbn, int edition, double price, ArrayList<Author> Author) { this.title = title; this.isbn = isbn; this.edition = edition; this.price = price; this.Author = Author; } public void sortArray(Book n){ Collections.sort(n, title); } public String getTitle() { return title; } public int getIsbn() { return isbn; } public int getEdition() { return edition; } public double getPrice() { return price; } public ArrayList<Author> getAuthors() { return Author; } public int compareTo(Book objekt) { if (objekt.getTitle().compareTo(this.getTitle()) < 0){ return 0; }else{ return 1; } } }
package se.kth.labb3b; import java.io.Serializable; public class Author implements Serializable { private String name; public Author(String n){ name = n; } public String getAuthor(){ return name; } }
Would really appreciate some help =)