Sort Array List of Objects in Java
In this tutorial, we’ll learn how to sort an Array List of Objects in Java
Build a List of Objects
Let’s understand the sorting of an Array List of Objects with an Example. Let’s create a class Book
:-
@Data
public class Book {
private final String name;
private final String author;
private final double rating;
}
Let’s create some Book
objects and build a library with an Array List of Book Objects:-
Book book1 = new Book("book1", "author1", 3.0);
Book book2 = new Book("book2", "author2", 5);
Book book3 = new Book("book3", "author1", 4.0);
Book book4 = new Book("book4", "author2", 2.5);
Book book5 = new Book("book5", "author1", 4.0);
List<Book> library = Arrays.asList(book1, book2, book3, book4, book5);
Sort List of Objects using Collections.sort()
We use Collections.sort()
method to sort the existing list of objects using Comparators. This sorting takes place on the same list of objects and no new list is created. If you want to keep the existing list as it is after sorting then use Streams instead, which generates a new sorted list.
Sort in ascending order
Sort a list of book objects by author in ascending order (author name in alphabetical increasing order)
Collections.sort(library, Comparator.comparing(Book::getAuthor));
System.out.println(library);
// [Book(name=book1, author=author1, rating=3.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book5, author=author1, rating=4.0),
// Book(name=book2, author=author2, rating=5.0),
// Book(name=book4, author=author2, rating=2.0)]
Sort in descending order
Sort a list of book objects by rating in descending order (rating number in decreasing order)
Collections.sort(library, Comparator.comparingDouble(Book::getRating).reversed());
System.out.println(library);
// [Book(name=book2, author=author2, rating=5.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book5, author=author1, rating=4.0),
// Book(name=book1, author=author1, rating=3.0),
// Book(name=book4, author=author2, rating=2.0)]
Sort by multiple fields in ascending order
Sort a list of book objects first by author name in alphabetical increasing order, then rating in increasing order and then by book name in alphabetical increasing order.
Collections.sort(library, Comparator.comparing(Book::getAuthor)
.thenComparing(Book::getRating)
.thenComparing(Book::getName));
System.out.println(library);
// [Book(name=book1, author=author1, rating=3.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book5, author=author1, rating=4.0),
// Book(name=book4, author=author2, rating=2.0),
// Book(name=book2, author=author2, rating=5.0)]
Sort by multiple fields in ascending then descending order
Sort a list of book objects by author name in alphabetical increasing order, then rating in decreasing order and then by book name in alphabetical decreasing order.
Collections.sort(library, Comparator.comparing(Book::getAuthor)
.thenComparing((b1, b2) -> Double.compare(b2.getRating(), b1.getRating()))
.thenComparing((b1, b2) -> b2.getName().compareTo(b1.getName())));
System.out.println(library);
// [Book(name=book5, author=author1, rating=4.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book1, author=author1, rating=3.0),
// Book(name=book2, author=author2, rating=5.0),
// Book(name=book4, author=author2, rating=2.0)]
Sort List of Objects using Streams
Use Java Streams for sorting the list of Objects when you don’t want to change the existing list and generate a new sorted list instead.
Sort in ascending order
Sort a list of book objects by author in ascending order
List<Book> sortedLibrary = library.stream()
.sorted(Comparator.comparing(Book::getAuthor))
.collect(Collectors.toList());
System.out.println(sortedLibrary);
// [Book(name=book1, author=author1, rating=3.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book5, author=author1, rating=4.0),
// Book(name=book2, author=author2, rating=5.0),
// Book(name=book4, author=author2, rating=2.0)]
Sort in descending order
Sort a list of book objects by rating in descending order
List<Book> sortedLibrary = library.stream()
.sorted(Comparator.comparingDouble(Book::getRating)
.reversed())
.collect(Collectors.toList());
System.out.println(sortedLibrary);
// [Book(name=book2, author=author2, rating=5.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book5, author=author1, rating=4.0),
// Book(name=book1, author=author1, rating=3.0),
// Book(name=book4, author=author2, rating=2.0)]
Sort by multiple fields in ascending order
Sort a list of book objects by author, then rating and then name in ascending order
List<Book> sortedLibrary = library.stream()
.sorted(Comparator.comparing(Book::getAuthor)
.thenComparingDouble(Book::getRating)
.thenComparing(Book::getName))
.collect(Collectors.toList())
System.out.println(sortedLibrary);
// [Book(name=book1, author=author1, rating=3.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book5, author=author1, rating=4.0),
// Book(name=book4, author=author2, rating=2.0),
// Book(name=book2, author=author2, rating=5.0)]
Sort by multiple fields in ascending then descending order
Sort a list of book objects by author in ascending, then rating in descending and then name in descending order
List<Book> sortedLibrary = library.stream()
.sorted(Comparator.comparing(Book::getAuthor)
.thenComparing((b1, b2) -> Double.compare(b2.getRating(), b1.getRating()))
.thenComparing((b1, b2) -> b2.getName().compareTo(b1.getName())))
.collect(Collectors.toList())
System.out.println(sortedLibrary);
// [Book(name=book5, author=author1, rating=4.0),
// Book(name=book3, author=author1, rating=4.0),
// Book(name=book1, author=author1, rating=3.0),
// Book(name=book2, author=author2, rating=5.0),
// Book(name=book4, author=author2, rating=2.0)]