Sorting ArrayList of custom objects by field in Java
Hello everyone! I'm new to Java and encountered a problem when sorting a list of objects. I have a `Book` class with fields `title` (String) and `year` (int). I store several books in `ArrayList`, and now I need to sort this list by year of publication — from oldest to newest.
I've heard about `Collections.sort()`, but it only works for lists of numbers or strings. When I try to apply it to my list of books, the compiler gives an error: "The method sort(List) in the type Collections is not applicable for the arguments (ArrayList)".
Here's my code for example:
```java
import java.util.ArrayList;
import java.util.Collections;
public class Book {
String title;
int year;
public Book(String title, int year) {
this.title = title;
t