0

Hello Would like to know how I can sort this arrayList

public class Librarian {
    public static void main(String[] args){

        Library library = new Library();



        library.addBook(new FictionBook("The walk through the exam", "Andreas", 0));
        library.addBook(new FictionBook("The incredible Programmer", "John", 1));
        library.addBook(new FictionBook("The Calculator", "Pius", 1));
        library.addBook(new FictionBook("The gozzilla", "Henry", 1));
        library.addBook(new FictionBook("The game", "Pele", 0));
        library.addBook(new FictionBook("Racing on the moon", "Marco",0));
        library.addBook(new FictionBook("London Show", "William", 0));
        library.addBook(new FictionBook("Water fights", "Claudia", 1));
        library.addBook(new FictionBook("Monster and Dragons", "Woozer", 1));
        library.addBook(new FictionBook("Pencils and pins", "Xian", 0));

        for(FictionBook myFictionBook : library.library){
            System.out.println(myFictionBook.getAuthor());
        }
    }
4
  • 5
    Comparable Interface... Commented Jan 23, 2014 at 11:50
  • Implement the Comparable interface for your FictionBook class and then use: Collection.sort(library); Commented Jan 23, 2014 at 11:50
  • Either let your FictionBook class implement the Comparable interface or provide a custom comparator when using Collections.sort() Commented Jan 23, 2014 at 11:51
  • There is no ArrayList in your code. You need to show the required code + show some effort on what you tried, or you won't get any useful answers here. Commented Jan 23, 2014 at 11:52

3 Answers 3

2

As the other mentioned you can implement the Comparable Interface in your java class. A better alternative would be the use of an external Comparator.

Comparator<FictionBook> comparator = new Comparator<FictionBook>() {

    public int compare(FictionBook a, FictionBook b) {
       return a.title.compareTo(b.title);
    }

}

If you now want to sort you books according to the release year you then only have to implement an other comparator and just use the new one. Or you write an DecoratorComparator which inverses the reuslt of the inner result.

Inverse sorter:

Comparator<FictionBook> inverse = new Comparator<FictionBook>() {

    public int compare(FictionBook a, FictionBook b) {
       return comparator.compare(a, b) * -1;
    }

}

The actual sorting is also done with Collections.sort(list, comparator). If you want a flexible sort solution use comparator.

Sign up to request clarification or add additional context in comments.

Comments

0

Sorting in Java is done by implementing the Comparable interface, which basically dictates how you compare two objects (i.e., which is "larger" and should be last, and which is "smaller" and should be first). After you do that, Collections.sort should take care of everything else.

Assuming your FictionBook class has a getTitle() method, you'd want to do something like this:

public class FictionBook implements Comparable<FictionBook> {
    // snipped...

    @Override
    public int compareTo(FictionBook other) {
        return getTitle().compareTo(other.getTitle());
    }
}

Then, you could just use Collections.sort(library).

An alternate approach would be to decide that FictionBooks have no natural ordering (i.e., they are not Comparable), but that another class handles the order. This can be done by implementing a Comparator in a similar fashion:

public class FictionBookComparator implements Comparator<FictionBook> {
    @Override
    public int compare(FictionBook o1, FictionBook o2) {
        return o1.getTitle().compareTo(o2.getTitle());
    }
}

Now, you can use this Comparator when sorting your library: Collections.sort(library, new FictionBookComparator()).

Comments

0

Implement a Comparable for FictionBook and then just sort library.library list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.