user3207868
user3207868

Reputation:

Sorting the arraylist alphabetically

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());
        }
    }

Upvotes: 0

Views: 90

Answers (3)

morpheus05
morpheus05

Reputation: 4872

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.

Upvotes: 2

Mureinik
Mureinik

Reputation: 311163

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()).

Upvotes: 0

pisek
pisek

Reputation: 1439

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

Upvotes: 0

Related Questions