简体   繁体   中英

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

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.

FictionBook实现Comparable,然后只对library.library列表进行排序。

Sorting in Java is done by implementing the Comparable interface, which basically dictates how you compare two objects (ie, 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 FictionBook s have no natural ordering (ie, 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()) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM