Julio Ripoll Moreno Blog

Bienvenido a mi blog

Java 8 - Lambda: Ordenar lista de objetos

Cómo ordenar lista de objetos con Java8 por una propiedad concreta.

class Book {
    String title;
    String author;
    Date date; 
}
Book book1 = new Book("Refactoring", "Martin Fowler", new Date());
Book book2 = new Book("Clean code", "Robert C. Martin", new Date());
Book book3 = new Book("Test Driven Development", "Kent Beck", new Date());

List<Book> books = Arrays.asList(book2, book1, book3);

books.stream().sorted((bookObject1, bookObject2) -> 
    bookObject1.getHeader().getDate().compareTo(bookObject2.getHeader().getDate()))
        .collect(Collectors.toList());
comments powered by Disqus