0

Reposting hopefully it'll be clearer

I need to sort the book titles accordingly based on the user's input and print them out accordingly. I have completed and managed to print all the books the user has entered but they are not in alphabetical order

These are my codes

    import java.util.ArrayList;
    import java.util.Collections;


    /**
     *
     * @author user
     */
    public class BookShelf {

        ArrayList<Book> listOfBooks = new ArrayList<Book>();

        public void addBook(Book book) {
            listOfBooks.add(book);
        }

        public ArrayList<Book> returnListOfBooks() {

            ArrayList<Book> myBook = new ArrayList<Book>(listOfBooks);  
            Collections.sort(myBook);
            return myBook;

        }
}

The ArrayList<Book> myBook = new ArrayList<Book>(listOfBooks); is to return those books i have added into the array list and then I will have to sort them accordingly however, I have this error on Collections.sort(myBook);

enter image description here

I have no idea how to fix this. Any help is appreciated!

Rind
  • 175
  • 1
  • 3
  • 14
  • 1
    It looks like your `Book` class doesn't implement `Comparable`. – azurefrog Nov 18 '15 at 19:20
  • And also http://stackoverflow.com/questions/11775980/sorting-a-list-of-objects-based-on-1-of-its-fields, and many, many, many duplicates. – JB Nizet Nov 18 '15 at 19:21
  • According to which data member of Book class you want to sort them – Arpit Agrawal Nov 18 '15 at 19:22
  • er, sorry still quite new with Java and programming terms but i guess data member which i would like to sort them is the title of the book which the user types in. – Rind Nov 18 '15 at 19:25
  • Check that [question](http://stackoverflow.com/questions/17739720/inferred-type-is-not-a-valid-substitute-for-a-comparable-generic-type). It may explain what you need – Juan Rivillas Nov 18 '15 at 19:25

0 Answers0