-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBook.java
More file actions
45 lines (29 loc) · 969 Bytes
/
Book.java
File metadata and controls
45 lines (29 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Arrays;
/*Sort the book objects with authors using Comparable interface */
public class Book implements Comparable<Book>{
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
public int compareTo(Book b) {
return this.author.compareTo(b.author);
}
public static void main(String[] args) {
Book[] books = new Book[3];
books[0] = new Book("Selfish Gene", "Richard Dawkins");
books[1] = new Book("Anna Karenina", "Leo Tolstoy");
books[2] = new Book("Great Expectations", "Charles Dawkins");
Arrays.sort(books);
for (Book book : books) {
System.out.println(book.getTitle() + ", " + book.getAuthor());
}
}
}